Rails query interface where clause issue?

六眼飞鱼酱① 提交于 2020-01-24 01:08:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!