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

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

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:
	http://wiki.rubyonrails.org/rails/pages/HowtoPagination

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:

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

HTH,
Kevin
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:
>       http://wiki.rubyonrails.org/rails/pages/HowtoPagination

Gracefull thanks, You make my day.

Ciao'
Mathieu
This topic is locked and can not be replied to.