Forum: Ruby on Rails Paginate do a Count(*) request before, how access that count

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.
Mathieu C. (Guest)
on 2005-12-29 11:10
(Received via mailing list)
Hello,

AV::paginate query first by a count, how access this value in the
controller or view ?

Why ?

Because I'll like to display the total number or records found w/o
doing another expenssive Count.

Thanks,
=?ISO-8859-1?Q?Thibaut_Barr=E8re?= (Guest)
on 2005-12-29 12:07
(Received via mailing list)
Hi Mathieu

maybe you could use a cached counter value in the database, as shown in
the
rails-blog-in-15-minutes-video ? (a bit different from what you exactly
describe, but less expensive as well)

hope this helps

Thibaut
=?ISO-8859-1?Q?Thibaut_Barr=E8re?= (Guest)
on 2005-12-29 12:07
(Received via mailing list)
Unless of course you need to query with conditions :)
Mathieu C. (Guest)
on 2005-12-29 13:02
(Received via mailing list)
> Unless of course you need to query with conditions :)

Plenty of conditions. up to 3 where id in (select x tables) ;)
Adam D. (Guest)
on 2005-12-29 15:56
(Received via mailing list)
here is how i do it

 page = (@params[:page] ||= 1).to_i
 perpage = (@session[:perpage] ||= 5).to_i
 offset = (page - 1) * perpage

 @messages = Message.find_by_sql(["select *,date_format(created_on,'%b
%e
%h:%i %p') as fancydate  from messages where user_id = ? order by
created_on
desc ",@session[:userid]])
 @total = @messages.size.to_i
 @message_pages = Paginator.new(self,@messages.length,perpage,page)
 @messages = @messages[offset..(offset + perpage - 1)]

 @first_msg = @message_pages.current.first_item
 @last_msg = @message_pages.current.last_item

 @listing = 'Listing ' + @first_msg.to_s + ' - ' + @last_msg.to_s + ' of
' +
@total.to_s
 This just displays "Listing 1-9 of 100"

hope it helps.
adam
This topic is locked and can not be replied to.