Paginate do a Count(*) request before, how access that count

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,

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

Unless of course you need to query with conditions :slight_smile:

Unless of course you need to query with conditions :slight_smile:

Plenty of conditions. up to 3 where id in (select x tables) :wink:

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