25

I get a list of objects from my Rails app, and use will_paginate to page as usual, and then I have a little method used to save details of the search to the database:

per_page=10
session[:search_params] = params[:search_people]
@documents = Person.search_people(params[:search_people], params[:page], per_page)

Search.create(:user_id     => (!current_user ? 0 : current_user.id),
   :search_type => "Person", 
   :firstname   => params[:search_people][:first_name], 
   :lastname    => params[:search_people][:last_name],
   :results     => @documents.count )

The problem is, the number of search results (@douments.count) is always <= per_page used for will_paginate.

I understand why this is, but is there a way around it without running the query twice, once with will_paginate and once without?

Christian Mayne
  • 1,709
  • 7
  • 26
  • 42

1 Answers1

72

Try <%=@documents.total_entries%>

Yunwei.W
  • 1,589
  • 1
  • 14
  • 31
  • Brilliant, thank you. Will accept in 9 minutes, when SO allows – Christian Mayne Jan 18 '13 at 15:49
  • Unfortunately that means a second SQL query, any way to do it without that? – bcackerman Oct 09 '14 at 13:15
  • 1
    hi @bcackerman, as long as you use `will_paginate` it will basically do 2 things each time: 1. load results for current page. 2. get the total count. So, this is when `total_entries` is initialised. and that's how those page numbers are calculated (total/per_page). So, its already initialised before you call it. No more query. – Yunwei.W Oct 11 '14 at 05:54