Retrieve the total number of records when paginating with will_paginate

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 Over 13 years ago