0

我目前有一个搜索表单,可以搜索我的食谱模型中的列,我现在想搜索一个关联模型也称为国家,并搜索名称列

所以到目前为止我的代码是

  q = "%#{params[:search]}%"

 @countrysearch = Recipe.where("dish_name LIKE ? OR country_of_origin LIKE ? OR difficulty LIKE ? OR preperation_time LIKE?", q, q, q, q )

我是否正确地说我需要加入国家模式?如果是这样,我不确定它的语法,有没有人知道要查看哪些资源或以前执行过此查询?

谢谢

4

2 回答 2

2

有一个名为ransack https://github.com/ernie/ransack的宝石允许这样做(https://github.com/ernie/ransack#has_many-and-belongs_to-associations

于 2012-11-11T16:24:08.090 回答
1

您可以使用连接,也可以预先加载关联并在其上指定条件:

Recipe.includes(:country).where("dish_name like ? OR countries.name like ?", ...)

请参阅本指南中的“指定预先加载的关联的条件”部分。

于 2012-11-11T16:10:42.913 回答