2013-05-16 64 views
0

我是嘗試如下Rails查詢接口where子句問題?

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

我正在一個MySQL錯誤作爲運行SQL查詢:

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

誰能幫我在這
由於事先

回答

1

你必須使用mysql的IN子句

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

你能幫我看看這個查詢嗎?請按照鏈接http://pastebin.com/2vup4PWD –

+0

@applicants = Applicant.includes(:jobs).where(「applicants.first_name LIKE?AND applicants.location LIKE?AND jobs.job_title LIKE?」,「%#{人們%「,」%#{位置}%「,」%#{more_job}%「) – Salil

+0

謝謝,它適用於我,但有點變化。而不是包括(:工作),我用包括(:工作) –

3

如果你想傳遞一個數組應該是最好編寫

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

或者使用squeel寶石。

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

你能幫我在這個查詢。請按照鏈接pastebin.com/2vup4PWD –

+0

你不能使用面向對象的符號applicants.job.job_title,因爲據我所知,job_title是一個工作表的列。你需要加入作業表。 http://guides.rubyonrails.org/active_record_querying.html#joining-tables –