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

Changeset 7347

Show
Ignore:
Timestamp:
08/20/07 04:26:10 (1 year ago)
Author:
nzkoz
Message:

Merge 8801 fix to stable. Closes #8801

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-stable/activerecord/CHANGELOG

    r7328 r7347  
    11*SVN* 
     2 
     3* Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo] 
    24 
    35* Don't clobber includes passed to has_many.count [danger] 
  • branches/1-2-stable/activerecord/lib/active_record/associations/has_many_through_association.rb

    r6410 r7347  
    102102        calculate(:sum, *args, &block) 
    103103      end 
     104       
     105      def count(*args) 
     106        column_name, options = @reflection.klass.send(:construct_count_options_from_legacy_args, *args) 
     107        if @reflection.options[:uniq] 
     108          # This is needed becase 'SELECT count(DISTINCT *)..' is not valid sql statement. 
     109          column_name = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key}" if column_name == :all 
     110          options.merge!(:distinct => true)  
     111        end 
     112        @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }  
     113      end 
    104114 
    105115      protected 
  • branches/1-2-stable/activerecord/test/associations/join_model_test.rb

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