From cccbedc0797c0142e8d23f2b81a95da696fdd4a5 Mon Sep 17 00:00:00 2001 From: Denis Barushev Date: Thu, 15 May 2008 21:11:22 +0300 Subject: [PATCH] Remove :include option from count_all query when it's possible. --- lib/will_paginate/finder.rb | 12 +++++++++--- 1 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/will_paginate/finder.rb b/lib/will_paginate/finder.rb index 6aac7e6..335a72e 100644 --- a/lib/will_paginate/finder.rb +++ b/lib/will_paginate/finder.rb @@ -184,8 +184,17 @@ module WillPaginate unless options[:select] and options[:select] =~ /^\s*DISTINCT\b/i excludees << :select # only exclude the select param if it doesn't begin with DISTINCT end + + # we may be in a model or an association proxy! + klass = (@owner and @reflection) ? @reflection.klass : self + # count expects (almost) the same options as find count_options = options.except *excludees + + # remove :include option if it doesn't need + if count_options[:include] and !klass.send :references_eager_loaded_tables?, count_options + count_options.delete :include + end # merge the hash found in :count # this allows you to specify :select, :order, or anything else just for the count query @@ -194,9 +203,6 @@ module WillPaginate # we may have to scope ... counter = Proc.new { count(count_options) } - # we may be in a model or an association proxy! - klass = (@owner and @reflection) ? @reflection.klass : self - count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with')) # scope_out adds a 'with_finder' method which acts like with_scope, if it's present # then execute the count with the scoping provided by the with_finder -- 1.5.4.3