will_paginate and record count

Hi, I have just installed will_paginate plugin and got it working OK.

Excuse me if this is a very obvious question but how do you get access to the total record count from all pages.

ie I want to display "nnn matching records"... followed by the <%= will_paginate @records %>

With the old rails pagination I am using count_collection_for_pagination which I always thought was a bit tacky.

Cheers Giorgio

Use the count method in AR, ex: User.count

Rails api docs is a good reference.

Surely the will_paginate plugin must have its own count? I could easily do a count on the database but that is an extra call that presumably the will_paginate has already done?

G.

Looking through the tests in will_paginate plugin, I think you can use the size method.

def test_simple_paginate

entries = Topic.paginate :page => nil

assert_equal 1, entries.current_page

assert_nil entries.previous_page

assert_nil entries.next_page

assert_equal 1, entries.page_count

assert_equal 4, entries.size

entries = Topic.paginate :page => 2

assert_equal 2, entries.current_page

assert_equal 1, entries.previous_page

assert_equal 1, entries.page_count

assert entries.empty?

:page parameter in options is required!

assert_raise(ArgumentError){ Topic.paginate }

assert_raise(ArgumentError){ Topic.paginate({}) }

end

Enjoy!

Try using the total_entries method

@posts = Post.paginate_by_board_id @board.id, :page => params[:page], :order => 'updated_at DESC'

total_records = @posts.total_entries