How to override count_collection_for_pagination in rails?

Elliott Blatt wrote the following on 24.11.2006 19:42 :


My task seems simple, but apparently there is some gap in my


  I'm working with an app that has a large number of records. (2.2
million). The problem is that these records need to be paged. The Rails
paginiate method first does a "SELECT count(*) ..." to determine the
size of the results set. Believe it or not, simply selecting the cound
on 2.2 million records, takes about 1.4 seconds. Which is actually quite
noticible to the user.

My solution:

So I'd like to work around this problem to by maintainng the count in a
special table just for this purpose.
Rails provides the module: "ActionController::Pagination"
Which contains several Protected Instance methods

On such method is: count_collection_for_pagination.

The rails doc says: "Override this method to implement a custom
This is what I've tried to do to override the rails method:
I put the following in my application_helper.rb
This is an odd place to put such a thing. Helper methods are available
in the view context (the corresponding modules are mixed in the views).
I don't know why the count_collection_for_pagination redefinition isn't
working here though (unless your code is in the ApplicationHelper module
namespace instead of the root one).

Just try to put the same code in your environment.rb (or one file it loads).