[英]Rails tire(elasticsearch) search through has_many association
我有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
方法中明确定义该to_indexed_json
。
另外, 轮胎已经退役。 您应该考虑迁移到elasticsearch -rails或我首选的宝石searchkick !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.