Rails tire(elasticsearch) search through has_many association -
i have 2 associations
belongs_to :author has_many :favorites
i'm wondering why example works:
tire.search(load: {include: [:author, :comments]}, page: params[:page], per_page: 8) query { string params[:query], default_operator: "and" } if params[:query].present? filter :term, author_id: ['111'] sort { :created_at, 'desc' } end
and 1 doesnt:
tire.search(load: {include: [:author, :comments]}, page: params[:page], per_page: 8) query { string params[:query], default_operator: "and" } if params[:query].present? filter :term, favorite_ids: ['567'] sort { :created_at, 'desc' } end
can me?
foreign keys stored in child table, rails joining of 2 tables you.
so in model, there attribute author_id
because model belongs to author. foreign keys favorites relationship stored in favorites table. while can model.first.favorites
, corresponding favorites
, because of value stored in latter table.
model.first.author_id
exists. model.first.favorite_ids
not.
if want run search on favorites_ids
, you're going need explicitly define in to_indexed_json
method.
also, tire has been retired. should migrating elasticsearch-rails, or preferred gem, searchkick!
Comments
Post a Comment