aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb9
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb15
-rw-r--r--activerecord/test/models/author.rb1
4 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index cb17b6e483..a1c8bd6e50 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -55,6 +55,12 @@ module ActiveRecord
end
end
+ class HasManyThroughCantCountOnColumnForGroupedAssociation < ActiveRecordError #:nodoc:
+ def initialize(owner, reflection, column_name)
+ super("Cannot count on column '#{column_name}' for association '#{owner.class.name}##{reflection.name}' grouped by '#{reflection.options[:group]}'.")
+ end
+ end
+
class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
def initialize(reflection)
super("Can not eagerly load the polymorphic association #{reflection.name.inspect}")
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 1ca44d645c..96d0b73da2 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -124,6 +124,15 @@ module ActiveRecord
column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" if column_name == :all
options.merge!(:distinct => true)
end
+
+ if @reflection.options[:group]
+ unless column_name == :all
+ raise HasManyThroughCantCountOnColumnForGroupedAssociation.new(@owner, @reflection, column_name)
+ end
+ column_name = @reflection.options[:group]
+ options.merge!(:distinct => true)
+ end
+
@reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }
end
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index f59fe70654..8da13d8b1b 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -603,6 +603,21 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
end
end
+ def test_group_has_many_through_should_use_group_for_count
+ using_length = authors(:david).reload.grouped_comments.length # all associated comments are read first
+ using_count = authors(:david).reload.grouped_comments.count # associated comments are only counted
+ assert_equal using_count, using_length
+
+ commented_posts = authors(:david).comments.map(&:post).uniq.size # count commented posts manually
+ assert_equal commented_posts, authors(:david).grouped_comments.count
+ end
+
+ def test_group_has_many_through_should_not_allow_column_name_for_count
+ assert_raises ActiveRecord::HasManyThroughCantCountOnColumnForGroupedAssociation do
+ authors(:david).grouped_comments.count(:id)
+ end
+ end
+
private
# create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency)
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index 593d77342e..64e4d38114 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -20,6 +20,7 @@ class Author < ActiveRecord::Base
has_many :funky_comments, :through => :posts, :source => :comments
has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
+ has_many :grouped_comments, :through => :posts, :source => :comments, :group => 'comments.post_id'
has_many :special_posts
has_many :special_post_comments, :through => :special_posts, :source => :comments