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

Changeset 7199

Show
Ignore:
Timestamp:
07/20/07 00:10:06 (1 year ago)
Author:
rick
Message:

Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r7192 r7199  
    11*SVN* 
     2 
     3* Fix #count on a has_many :through association so that it recognizes the :uniq option.  Closes #8801 [lifofifo] 
    24 
    35* Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo] 
  • trunk/activerecord/lib/active_record/associations/has_many_through_association.rb

    r7182 r7199  
    101101        calculate(:sum, *args, &block) 
    102102      end 
     103       
     104      def count(*args) 
     105        column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args) 
     106        if @reflection.options[:uniq] 
     107          # This is needed becase 'SELECT count(DISTINCT *)..' is not valid sql statement. 
     108          column_name = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key}" if column_name == :all 
     109          options.merge!(:distinct => true)  
     110        end 
     111        @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }  
     112      end 
    103113 
    104114      protected 
  • trunk/activerecord/test/associations/join_model_test.rb

    r7182 r7199  
    3131    assert_equal 1, authors(:mary).unique_categorized_posts.size 
    3232  end 
    33  
     33   
     34  def test_has_many_uniq_through_count 
     35    author = authors(:mary) 
     36    assert !authors(:mary).unique_categorized_posts.loaded? 
     37    assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count } 
     38    assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count(:title) } 
     39    assert_queries(1) { assert_equal 0, author.unique_categorized_posts.count(:title, :conditions => "title is NULL") } 
     40    assert !authors(:mary).unique_categorized_posts.loaded? 
     41  end 
     42   
    3443  def test_polymorphic_has_many 
    3544    assert posts(:welcome).taggings.include?(taggings(:welcome_general))