Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# add this file as config/initializers/will_paginate_postgresql_count.rb
# in a Rails application
module WillPaginate
module Finder
module ClassMethods
# add '1=1' to paginate conditions as marker such that the select from the pg_class
# is used to approximate simple rows count, e.g.
# Foo.paginate(:page => params[:page], :conditions => "1=1")
def wp_count_with_postgresql(options, args, finder, &block)
if options[:conditions] == ["1=1"] || options[:conditions] == "1=1"
# counting rows in PostgreSQL is slow so use the pg_class table for
# approximate rows count on simple selects
self.count_by_sql "SELECT (reltuples)::integer FROM pg_class r WHERE relkind = 'r' AND relname = '#{self.table_name}'"
wp_count_without_postgresql(options, args, finder, &block)
alias_method_chain :wp_count, :postgresql

This comment has been minimized.

Copy link

commented Dec 14, 2010

There's a problem with the alias_method_chain. Since wp_count is protected, it doesn't seem to work. here's the error:

...aliasing.rb:33:in alias_method': undefined methodwp_count' for module `WillPaginate::Finder::ClassMethods' (NameError)

Any ideas how to alias the protected method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.