Searching has_many association with Searchkick

此生再无相见时 提交于 2019-12-13 05:36:52

问题


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?


回答1:


@results = current_user.items.search('Query')

It's not possible to search like this. and if it was then it'll hit the performance to query from both data sources and get intersect of the results set. so start from the item end and include user info to that model.

class Item
 searchkick word_middle: %i[name]
 def search_data
   {
     consumer_type: consumer.class.to_s
     consumer_id: consumer.id.to_s
     name: self.name
   }
 end
end

options = {where: [{consumer_type: 'User', consumer_id: current_user.id.to_s}]}
results = Item.search 'query', options


来源:https://stackoverflow.com/questions/53876417/searching-has-many-association-with-searchkick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!