将分页:限制结果数量

发布于 2024-12-08 00:12:48 字数 111 浏览 0 评论 0原文

我正在使用 ruby​​ 的 will paginate gem。我正在使用将分页来获取一群人并在字段上进行排序。我想要的只是其中的前 100 个。本质上都是顶尖人物。我似乎无法做到这一点。我该怎么办呢?谢谢

Im using the will paginate gem for ruby. I am using will paginate to get a bunch of people and sorting on a field. What I want is only the first 100 of those. Essentially the top people. I cant seeem to do this. How would i go about it? Thanks

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

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

发布评论

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

评论(3

苏辞 2024-12-15 00:12:48

据我所知, will_paginate 没有为此提供选项,我也只是在其源代码中找到了一个根来检查,但是如果您不介意在您的条件中使用子查询,则可以完成...

people = Person.paginate(page: 10).where('people.id IN (SELECT id FROM people ORDER BY a_field LIMIT 100)').per_page(5)
people.total_pages
=> 20

替换a_field 与您排序的字段,这应该适合您。

(注意,上面使用当前版本的 will_paginate (3.0.2) 的语法,但相同的概念也适用于旧版本以及使用选项哈希)

As far as my knowledge goes will_paginate doesn't provide an option for this, I just had a root around in its source too to check, but if you don't mind having a subquery in your conditions it can be done...

people = Person.paginate(page: 10).where('people.id IN (SELECT id FROM people ORDER BY a_field LIMIT 100)').per_page(5)
people.total_pages
=> 20

Replace a_field with the field your sorting on, and this should work for you.

(note, the above uses the current version of will_paginate (3.0.2) for its syntax, but the same concept applies to older version as well using the options hash)

冧九 2024-12-15 00:12:48

will_paginate gem 将采用 total_entries 参数来限制分页的条目数量。

@problems = Problem.paginate(page: params[:page], per_page: 10, 
total_entries: 30)

这将为您提供 3 页,每页 10 条记录。

will_paginate gem will take total_entries parameter to limit the number of entries to paginate.

@problems = Problem.paginate(page: params[:page], per_page: 10, 
total_entries: 30)

This gives you 3 pages of 10 records each.

嘿哥们儿 2024-12-15 00:12:48
people = Person.limit(100).paginate(:per_page => 5)
people = Person.limit(100).paginate(:per_page => 5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文