问题
i am try to run the sql query as follows
@applicants = Applicant.where("applicants.first_name LIKE ? AND applicants.status = ?", "%#{people}%", ["new", "in-review"] )
I am getting an mysql error as :
ActionView::Template::Error (Mysql2::Error: Operand should contain 1 column(s): SELECT `applicants`.* FROM `applicants` WHERE (applicants.first_name LIKE '%sh%' AND applicants.status = 'new','in-review')):
can anyone help me in this
thanks in advance
回答1:
You have to use IN
clause of mysql
@applicants = Applicant.where("applicants.first_name LIKE ? AND
applicants.status in (?)",
"%#{people}%", ["new", "in-review"] )
回答2:
If you want pass an array it should be better to write
@applicants = Applicant
.where("applicants.first_name LIKE ?", "%#{people}%")
.where(status: ["new", "in-review"])
Or use squeel gem.
@applicants = Applicant.where{ (status.in(["new", "in-review"]) & (first_name =~ "%#{people}%") }
来源:https://stackoverflow.com/questions/16579238/rails-query-interface-where-clause-issue