will_paginate 生成错误数量的页面链接
我正在使用将对 3.0.2 和 Rails 3.1.0 进行分页。
以下代码位于我的控制器中。
@users = User.visible_for(current_user).
includes(:study_courses).
ordered_by_last_name.
page(params[:page]).per_page(20)
在上面的 @users
已分配给 users
的部分中,我这样做:
= will_paginate users, previous_label: h("<"), next_label: h(">")
如果有 20 个用户,它会给我 6 个页面链接,其中第一页包含 20 个用户,第二页包含 10 个用户,当然其余页面包含 0 个用户。
我不明白为什么生成了 6 个页面链接而不是 3 个。
更新: 发现will_paginate不使用distinct来计算记录。有什么想法如何做到这一点?
I am Using will paginate 3.0.2 and Rails 3.1.0.
The following code lives within my controller.
@users = User.visible_for(current_user).
includes(:study_courses).
ordered_by_last_name.
page(params[:page]).per_page(20)
In a partial where @users
has been assigned with users
from above I do:
= will_paginate users, previous_label: h("<"), next_label: h(">")
If there are 20 Users it gives me 6 page links, where the first page contains 20 users, the second page contains 10 users and of course the remaining pages contain zero users.
I can not figure out why there are 6 page links generated instead of 3.
UPDATE:
Figured out that will_paginate does not use distinct to count the records. Any ideas how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我自己找到了一个解决方案:
在我的范围内,我使用 distinct,但在关系上调用
count
似乎会覆盖它。因此,必须手动计数并将计数传递给分页方法。OK, I found a solution on my own:
In my scope I use disctinct, but calling
count
on the relation seems to overwrite that. So one has to count by hand and pass the count into the paginate method.这种方式看起来更干净: https://stackoverflow.com/a/10740732/2599681
我尝试过,并且与 Rails 配合得很好3.2.11 和 WillPaginate 3.0.4。
问候!
This way looks cleaner: https://stackoverflow.com/a/10740732/2599681
I tried it and works nicely with Rails 3.2.11 and WillPaginate 3.0.4.
Regards!
就我而言,
.count
也给出了错误的数字。因为.count
运行包含“distinct”的 SQL。我将其更改为.length
并且工作正常。像这样
In my case,
.count
gives wrong number, too. Because.count
runs SQL whitch includes "distinct". I changed it to.length
and it works fine.Like this