2013-01-19 104 views
0

我升級到Rails 3.2.3,突然這個代碼將不再有效:Where子句不再適用於Rails 3.2.3?

def self.search(query, project_id, person_id) 
    if query 
     where("number LIKE ?", "%#{query}%") 
    elsif project_id 
     where("project_id LIKE ?", project_id) 
    elsif person_id 
     where("projects.person_id = ?", person_id)  
    else 
     scoped 
    end 
    end 

這是最後where條款觸發錯誤:

SQLite3::SQLException: no such column: projects.person_id: SELECT COUNT(DISTINCT "invoices"."id") FROM "invoices" LEFT OUTER JOIN "items" ON "items"."invoice_id" = "invoices"."id" LEFT OUTER JOIN "payments" ON "payments"."invoice_id" = "invoices"."id" WHERE "invoices"."user_id" = 1 AND (projects.person_id = '1') 

在我的模型全部belongs_tohas_many語句設置正確,它在我以前版本的Rails中工作(不確定哪一個是通過)。

有人可以告訴我如何再次得到這個工作?

感謝您的任何幫助。

回答

1

我相信你將不得不加入projects表:

.. 
    elsif person_id 
     joins(:projects).where("projects.person_id = ?", person_id)  
    else 
.. 
+0

優秀的,謝謝!實際上,我不得不使用這個語法來使它工作:'連接(:項目)' – Tintin81