Paginate Multiple Models in Kaminari

后端 未结 3 941
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 18:14

I\'m creating a search page that will do an application wide search on users, posts, and comments. I currently have:

# POST /search
def index
  query = params[:q         


        
3条回答
  •  醉话见心
    2021-02-01 18:57

    Before thinking about a solution, you need to first define exactly what you want the final result to be. If you want to display a few of each type of record on the results page you can modify the approach you posted and combine the three paginated results using:

    @results = @users + @posts + @comments
    @results.sort! { |a, b| a.score(query) > b.score(query) }
    

    Each object will need to have an instance method 'score' that will let it sort based on the query priority. Also, you will need to modify your view to handle correct rendering of each item and ensure that the pagination is called on the model with the most pages.

    Alternatively, a more robust method would be to add a full-text search service (such as Index Tank, Web Solr, Thinking Sphinx). The technology for what's hot for these moves quickly, so do some research and find one that fits your needs. Example syntax for this would be something like:

    User.multi_solr_search query, models: [Post, Comment]
    

提交回复
热议问题