-
-
Save derencius/422661 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# http://wiki.postgresql.org/wiki/Slow_Counting | |
# http://www.varlena.com/GeneralBits/120.php | |
self.count_by_sql "SELECT (reltuples)::integer FROM pg_class r WHERE relkind = 'r' AND relname = '#{self.table_name}'" | |
else | |
wp_count_without_postgresql(options, args, finder, &block) | |
end | |
end | |
alias_method_chain :wp_count, :postgresql | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 method
wp_count' for module `WillPaginate::Finder::ClassMethods' (NameError)Any ideas how to alias the protected method?