Forum: Ruby on Rails Paginate do a Count(*) request before...

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Kevin S. (Guest)
on 2005-12-29 17:09
(Received via mailing list)

Paginate needs to know how many total records there are when you
create the object so you must either do an SQL COUNT or get all the
records and find the length of the array.  If it's a small data set,
I say find them all once and save yourself the second SQL query.  If
it's a large data set do the COUNT and then only find the records you
want which will save overhead by retrieving a small amount of data
even though it uses two queries.

I've put a page on the Rails Wiki which might help you:

To Adam, one tip about your solution.  Once you have a Paginator
object that the view can see, you can use its methods to get the
number of items while in the view instead of having to set the
variables @total, @first_msg, @last_msg in the controller.  (Everyone
has their own style but I like to keep my controller as streamlined
as I can.)  Just use something like this in your view:

<%= @message_pages.current.first_item %> -
<%= @message_pages.current.last_item %> of
<%= pluralize(@ message_pages.item_count, 'message') %>

Mathieu C. (Guest)
on 2005-12-29 17:15
(Received via mailing list)
>I've put a page on the Rails Wiki which might help you:

Gracefull thanks, You make my day.

This topic is locked and can not be replied to.