Kaminari中的分页多个模型 [英] Paginate Multiple Models in Kaminari

查看:103
本文介绍了Kaminari中的分页多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个搜索页面,该页面将在应用程序范围内对用户,帖子和评论进行搜索.我目前有:

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[:query]
  @users = User.search(query).page(params[:page])
  @posts = Post.search(query).page(params[:page])
  @comments = Comment.search(query).page(params[:page])

  respond_to do |format|
    format.html
  end
end

但是,我实际上是在尝试将所有结果混合在一起然后分页.进行分页搜索的策略有哪些?谢谢!

However I'm really trying to get something where all the results are mixed together then paginated. What are some of the strategies for doing paginated search like this? Thanks!

推荐答案

在考虑解决方案之前,您需要首先准确定义最终结果是什么.如果要在结果页面上显示每种记录的几种,则可以修改发布的方法,并使用以下三种方法组合三个分页的结果:

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) }

每个对象都需要有一个实例方法'score',该实例方法可以让它根据查询优先级进行排序.另外,您将需要修改视图以处理每个项目的正确渲染,并确保在页面数最多的模型上调用分页.

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.

或者,一种更可靠的方法是添加全文搜索服务(例如索引库 Web Solr 思维狮身人面像).针对这些热点的技术迅速发展,因此请进行一些研究,找到适合您需求的技术.示例语法如下:

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]

这篇关于Kaminari中的分页多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆