Retrieve the total number of records when paginating with will_paginate

Posted Over 13 years ago. Visible to the public.

When you use will_paginage Show archive.org snapshot to paginate a scope, and you want to obtain the total number of records matched before pagination, you can use total_entries:

users = User.active.paginate
puts users.count # number of records on this page, e.g. 50
puts users.total_entries # total number of records before pagination, e.g. one billion trillion

This will trigger a second database query in order to retrieve the count (which will run anyway if you render any kind of pagination widget).


will_paginate is not actually good in counting, it may take several seconds. See this note,

Henning Koch
Last edit
About 12 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2010-11-18 09:38)