aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2007-07-20 00:10:06 +0000
committerRick Olson <technoweenie@gmail.com>2007-07-20 00:10:06 +0000
commit3029a76af32113676182f902ad4f91d82c2734de (patch)
treea8d06bbeeccef27dade4955a72c901a100781add /activerecord/lib/active_record
parent9014bf3f262a6b55aa4a05e2626aeebf688aa05a (diff)
downloadrails-3029a76af32113676182f902ad4f91d82c2734de.tar.gz
rails-3029a76af32113676182f902ad4f91d82c2734de.tar.bz2
rails-3029a76af32113676182f902ad4f91d82c2734de.zip
Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7199 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index b75de4d23f..3d60da76b3 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -100,6 +100,16 @@ module ActiveRecord
def sum(*args, &block)
calculate(:sum, *args, &block)
end
+
+ def count(*args)
+ column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args)
+ if @reflection.options[:uniq]
+ # This is needed becase 'SELECT count(DISTINCT *)..' is not valid sql statement.
+ column_name = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key}" if column_name == :all
+ options.merge!(:distinct => true)
+ end
+ @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }
+ end
protected
def method_missing(method, *args, &block)