我升级到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_to
和has_many
语句都设置正确,并且它在我之前的Rails版本中有效(不确定是哪一个)。
有人能告诉我如何重新开始工作吗?
感谢您的帮助。
答案 0 :(得分:1)
我相信你必须加入projects
表:
..
elsif person_id
joins(:projects).where("projects.person_id = ?", person_id)
else
..