我有2个协会
belongs_to :author
has_many :favorites
我想知道为什么这个例子有效:
tire.search(load: {include: [:author, :comments]}, page: params[:page], per_page: 8) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
filter :term, author_id: ['111']
sort { by :created_at, 'desc' }
end
而这个没有:
tire.search(load: {include: [:author, :comments]}, page: params[:page], per_page: 8) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
filter :term, favorite_ids: ['567']
sort { by :created_at, 'desc' }
end
谁能帮我?
最佳答案
外键存储在子表中,Rails为您完成两个表的连接。
因此,在此模型中,存在一个author_id
属性,因为此模型属于作者。收藏夹关系的外键存储在收藏夹表中。尽管可以执行Model.first.favorites
并获取相应的favorites
,但这是因为后者表中存储了一个值。Model.first.author_id
存在。 Model.first.favorite_ids
没有。
如果要在favorites_ids
上运行搜索,则需要在to_indexed_json
方法中显式定义它。
此外,tire也已退休。您应该考虑迁移到elasticsearch-rails或我的首选 gem searchkick!
关于ruby-on-rails - 通过has_many关联搜索Rails轮胎(ElasticSearch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31883582/