I'm attempting to build out a search feature for my app and am running into some issues with being able to search a has_many association. I want it set up so that a user only sees items that they have access to.
In my user model, I have:
class User < ApplicationRecord
searchkick
has_many :items, -> (user) { unscope(:where).where("(consumer_type = ? and consumer_id = ?))", 'User', user.id) }, fully_load: false, dependent: :destroy
# not sure if this is helpful, was messing around with this
def search_data
{
name: items.name
}
end
end
Then in my Item model I have:
class Item < ApplicationRecord
belongs_to :consumable, polymorphic: true
belongs_to :consumer, polymorphic: true
end
Then I have two more models, "Book" and "Camera" that belong to Item. I need to be able to run a query in my controller such as:
@results = current_user.items.search('Query')
... so that it returns both Book and Camera results that match the search query. I read the Personalized Results section of the Searchkick docs, but was unable to get it to work at all. Any helpful tips, or has anybody achieved something similar scoping results to a specific user?