scoped_search icon indicating copy to clipboard operation
scoped_search copied to clipboard

Boolean fields with complete_value hash generates AR::PreparedStatementInvalid exceptions

Open jjeffers opened this issue 4 years ago • 0 comments

What search query did you use? Issuing a search for the name of the field, ie, a user model with a boolean "admin" field and a search term of "admin".

What is the search definition used on your models? Example here: https://github.com/theforeman/foreman/blob/4a61ebc9f2c7a41301672843eb96f1b265de5335/app/models/user.rb#L124

What do the tables involved in your search definition look like? Please include the type of the fields.

create_table "users", id: :serial, force: :cascade do |t|
    t.string "login", limit: 255
    t.string "firstname", limit: 255
    t.string "lastname", limit: 255
    t.string "mail", limit: 255
    t.boolean "admin", default: false, null: false
    t.datetime "last_login_on"
    t.integer "auth_source_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string "password_hash", limit: 128
    t.string "password_salt", limit: 128
    t.string "locale", limit: 5
    t.string "avatar_hash", limit: 128
    t.integer "default_organization_id"
    t.integer "default_location_id"
    t.string "lower_login", limit: 255
    t.boolean "mail_enabled", default: true
    t.string "timezone", limit: 255
    t.text "description"
    t.boolean "disabled", default: false
    t.index ["lower_login"], name: "index_users_on_lower_login", unique: true
  end

What was the SQL that scoped_search ended up generating? {:conditions=>["([\"(\\\"users\\\".\\\"admin\\\" <> ?)\", false])"]} which when passed to AR yields:

ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (0 for 1) in: (["(\"katello_content_views\".\"composite\" <> ?)", false])
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/sanitization.rb:209:in `raise_if_bind_arity_mismatch'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/sanitization.rb:166:in `replace_bind_variables'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/sanitization.rb:129:in `sanitize_sql_array'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/sanitization.rb:26:in `sanitize_sql_for_conditions'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/relation/where_clause_factory.rb:14:in `build'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/relation/query_methods.rb:656:in `where!'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/activerecord-6.0.3.2/lib/active_record/relation/query_methods.rb:649:in `where'
16:25:05 rails.1   |  27d751bf | /home/vagrant/foreman/.vendor/ruby/2.5.0/gems/scoped_search-4.1.8/lib/scoped_search/definition.rb:329:in `block in register_named_scope!'

In this case, the SQL array was converted to a string, etc.

Versions: scoped_search .18 postgres 12.1 actriverecord 6.0.3.2 ruby 2.5.5p157

jjeffers avatar Aug 17 '20 13:08 jjeffers