问题
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