I have 2 associations
belongs_to :author
has_many :favorites
I'm wondering why this example works:
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
And this one doesnt:
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
Can anyone help me?
Foreign keys are stored in the child table, Rails does the joining of the two tables for you.
So in this model, there is an attribute
author_idbecause this model belongs to an author. The foreign keys for the favorites relationship are stored in the favorites table. While you can doModel.first.favoritesand get the correspondingfavorites, it is because of a value stored in the latter table.Model.first.author_idexists.Model.first.favorite_idsdoes not.If you want to run a search on
favorites_ids, you're going to need to explicitly define that in theto_indexed_jsonmethod.Also, tire has been retired. You should look into migrating to elasticsearch-rails, or my preferred gem, searchkick!