Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Paginate: Limit number of results

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

like image 812
Jonovono Avatar asked Oct 01 '11 14:10

Jonovono


1 Answers

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)

like image 112
roboles Avatar answered Sep 21 '22 04:09

roboles