Rails查询接口where子句问题?

时间:2013-05-16 04:52:13

标签: mysql ruby-on-rails ruby-on-rails-3 activerecord

我尝试按如下方式运行sql查询

@applicants = Applicant.where("applicants.first_name LIKE ? AND applicants.status = ?", "%#{people}%", ["new", "in-review"] )

我收到一个mysql错误:

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')):

任何人都可以帮助我 提前谢谢

2 个答案:

答案 0 :(得分:3)

如果你想传递一个数组,最好写

@applicants = Applicant
    .where("applicants.first_name LIKE ?", "%#{people}%")
    .where(status: ["new", "in-review"])

或使用squeel gem。

@applicants = Applicant.where{ (status.in(["new", "in-review"]) & (first_name =~ "%#{people}%") }

答案 1 :(得分:1)

你必须使用mysql的IN子句

@applicants = Applicant.where("applicants.first_name LIKE ? AND 
                               applicants.status in (?)", 
                               "%#{people}%", ["new", "in-review"] )