Skip to content

Commit

Permalink
Revert r8742: remove has_many with :group option since it has sketchy…
Browse files Browse the repository at this point in the history
… sql support. Closes #10480.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8790 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Feb 3, 2008
1 parent 7fe2ad7 commit 78253ac
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 31 deletions.
6 changes: 0 additions & 6 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -55,12 +55,6 @@ def initialize(owner, reflection)
end end
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: class EagerLoadPolymorphicError < ActiveRecordError #:nodoc:
def initialize(reflection) def initialize(reflection)
super("Can not eagerly load the polymorphic association #{reflection.name.inspect}") super("Can not eagerly load the polymorphic association #{reflection.name.inspect}")
Expand Down
Expand Up @@ -124,15 +124,6 @@ def count(*args)
column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" if column_name == :all column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" if column_name == :all
options.merge!(:distinct => true) options.merge!(:distinct => true)
end 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) } @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }
end end


Expand Down
15 changes: 0 additions & 15 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -603,21 +603,6 @@ def test_belongs_to_shared_parent
end end
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 private
# create dynamic Post models to allow different dependency options # create dynamic Post models to allow different dependency options
def find_post_with_dependency(post_id, association, association_name, dependency) def find_post_with_dependency(post_id, association, association_name, dependency)
Expand Down
1 change: 0 additions & 1 deletion activerecord/test/models/author.rb
Expand Up @@ -20,7 +20,6 @@ def testing_proxy_target
has_many :funky_comments, :through => :posts, :source => :comments 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, :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 :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_posts
has_many :special_post_comments, :through => :special_posts, :source => :comments has_many :special_post_comments, :through => :special_posts, :source => :comments
Expand Down

0 comments on commit 78253ac

Please sign in to comment.