簡體   English   中英

通過has_many關聯搜索Rails輪胎(ElasticSearch)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM