Will Paginate: Limit number of results

可紊 提交于 2019-12-12 12:25:19

问题


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


回答1:


people = Person.limit(100).paginate(:per_page => 5)



回答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)




回答3:


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.



来源:https://stackoverflow.com/questions/7620978/will-paginate-limit-number-of-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!