aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_through_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-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)