Named scope - wrong number of binding variables

I have the following named scope:

named_scope :report_search, lambda { |search|
  {
    :conditions => ["rep_name like ? or rep_id like ? or rep_symbol like ? or rep_issue like ?", search]
  }
}

When I run it, I get an error message:

wrong number of bind variables

I would appreciate it if someone could help me figure out what is wrong with the code.

Many thanks

+5
source share
2 answers

You need one variable for each question mark in your binding application. If they are all the same, you need to repeat them several times:

named_scope :report_search, lambda { |search|
  {
    :conditions => ["rep_name like ? or rep_id like ? or rep_symbol like ? or rep_issue like ?", search, search, search, search]
  }
}
+8
source

You can use bind named variables if you do not want to repeat the input.

named_scope :report_search, lambda { |search|
  {
    :conditions => ["  rep_name   LIKE :search OR 
                       rep_id     LIKE :search OR 
                       rep_symbol LIKE :search OR 
                       rep_issue  LIKE :search", :search => search]
  }
}
+14
source

All Articles