在 Kaminari 中对多个模型进行分页

发布于 2024-11-15 01:09:59 字数 386 浏览 4 评论 0 原文

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

# 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

但是我真的很想得到一些将所有结果混合在一起然后分页的东西。进行这样的分页搜索有哪些策略?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咽泪装欢 2024-11-22 01:09:59

自从这次提交以来: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128

你可以执行以下操作

在你看来你可以这样做:

<%= paginate @users, :remote => true, :param_name => "user_page" %>
<%= paginate @posts, :remote => true, :param_name => "post_page" %>
<%= paginate @comments, :remote => true, :param_name => "comment_#{some_post_id}_page" %>

然后在你的控制器中你可以在这个中引用它们方式:

@users = User.search(query).page(params[:user_page])
@posts = Post.search(query).page(params[:post_page])
@comments = Comment.search(query).page(params[:comment_page])

和你的视图 js.erb 你可能有类似的东西:

$('#posts').html('<%= escape_javascript render(@posts) %>');
$('.table-pager').html('<%= escape_javascript(paginate(@posts, :remote => true).to_s) %>');

Ever since this commit: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128

You can do the following

In your view you can do this:

<%= paginate @users, :remote => true, :param_name => "user_page" %>
<%= paginate @posts, :remote => true, :param_name => "post_page" %>
<%= paginate @comments, :remote => true, :param_name => "comment_#{some_post_id}_page" %>

and then in your controller you can refer to them in this way:

@users = User.search(query).page(params[:user_page])
@posts = Post.search(query).page(params[:post_page])
@comments = Comment.search(query).page(params[:comment_page])

and your view's js.erb you might have something like:

$('#posts').html('<%= escape_javascript render(@posts) %>');
$('.table-pager').html('<%= escape_javascript(paginate(@posts, :remote => true).to_s) %>');
无边思念无边月 2024-11-22 01:09:59

在考虑解决方案之前,您需要首先准确定义您想要的最终结果。如果您想在结果页面上显示每种类型的记录中的一些,您可以修改您发布的方法并使用以下方法组合三个分页结果:

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

每个对象都需要有一个实例方法“score”,让它根据以下条件进行排序:查询优先级。此外,您还需要修改视图以处理每个项目的正确渲染,并确保在具有最多页面的模型上调用分页。

或者,更可靠的方法是添加全文搜索服务(例如 Index TankWeb Solr思考狮身人面像)。这些热门技术的发展速度很快,因此请进行一些研究并找到适合您需求的技术。示例语法如下:

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

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]
狂之美人 2024-11-22 01:09:59

您可以将查询结果与运行页面结合起来。

users = User.search(query)
posts = Post.search(query)
comments = Comment.search(query)
@results = users + posts + comments
@results.page(params[:page])

You could combine the results from the query and run page on that.

users = User.search(query)
posts = Post.search(query)
comments = Comment.search(query)
@results = users + posts + comments
@results.page(params[:page])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文