Changeset 8742
- Timestamp:
- 01/27/08 02:41:55 (2 years ago)
- Files:
-
- trunk/activerecord/lib/active_record/associations.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/has_many_through_association.rb (modified) (1 diff)
- trunk/activerecord/test/cases/associations/join_model_test.rb (modified) (1 diff)
- trunk/activerecord/test/models/author.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/associations.rb
r8707 r8742 53 53 def initialize(owner, reflection) 54 54 super("Cannot dissociate new records through '#{owner.class.name}##{reflection.name}' on '#{reflection.source_reflection.class_name rescue nil}##{reflection.source_reflection.name rescue nil}'. Both records must have an id in order to delete the has_many :through record associating them.") 55 end 56 end 57 58 class HasManyThroughCantCountOnColumnForGroupedAssociation < ActiveRecordError #:nodoc: 59 def initialize(owner, reflection, column_name) 60 super("Cannot count on column '#{column_name}' for association '#{owner.class.name}##{reflection.name}' grouped by '#{reflection.options[:group]}'.") 55 61 end 56 62 end trunk/activerecord/lib/active_record/associations/has_many_through_association.rb
r8571 r8742 125 125 options.merge!(:distinct => true) 126 126 end 127 128 if @reflection.options[:group] 129 unless column_name == :all 130 raise HasManyThroughCantCountOnColumnForGroupedAssociation.new(@owner, @reflection, column_name) 131 end 132 column_name = @reflection.options[:group] 133 options.merge!(:distinct => true) 134 end 135 127 136 @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) } 128 137 end trunk/activerecord/test/cases/associations/join_model_test.rb
r8681 r8742 604 604 end 605 605 606 def test_group_has_many_through_should_use_group_for_count 607 using_length = authors(:david).reload.grouped_comments.length # all associated comments are read first 608 using_count = authors(:david).reload.grouped_comments.count # associated comments are only counted 609 assert_equal using_count, using_length 610 611 commented_posts = authors(:david).comments.map(&:post).uniq.size # count commented posts manually 612 assert_equal commented_posts, authors(:david).grouped_comments.count 613 end 614 615 def test_group_has_many_through_should_not_allow_column_name_for_count 616 assert_raises ActiveRecord::HasManyThroughCantCountOnColumnForGroupedAssociation do 617 authors(:david).grouped_comments.count(:id) 618 end 619 end 620 606 621 private 607 622 # create dynamic Post models to allow different dependency options trunk/activerecord/test/models/author.rb
r8675 r8742 21 21 has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id' 22 22 has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC' 23 has_many :grouped_comments, :through => :posts, :source => :comments, :group => 'comments.post_id' 23 24 24 25 has_many :special_posts