Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8742

Show
Ignore:
Timestamp:
01/27/08 02:41:55 (2 years ago)
Author:
nzkoz
Message:

Make sure count works on has_many :through associations using :group. Closes #10480 [remvee]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/associations.rb

    r8707 r8742  
    5353    def initialize(owner, reflection) 
    5454      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]}'.") 
    5561    end 
    5662  end 
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r8571 r8742  
    125125          options.merge!(:distinct => true)  
    126126        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 
    127136        @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }  
    128137      end 
  • trunk/activerecord/test/cases/associations/join_model_test.rb

    r8681 r8742  
    604604  end 
    605605 
     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 
    606621  private 
    607622    # create dynamic Post models to allow different dependency options 
  • trunk/activerecord/test/models/author.rb

    r8675 r8742  
    2121  has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id' 
    2222  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' 
    2324 
    2425  has_many :special_posts