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

Changeset 9223

Show
Ignore:
Timestamp:
04/04/08 12:06:22 (3 months ago)
Author:
nzkoz
Message:

Don't double include DISTINCT when the user has already specified it. Closes #11502 [kenneth.kalmer]

Files:

Legend:

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

    r9043 r9223  
    170170          end 
    171171 
    172           sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}" 
     172          if options[:distinct] && column_name.to_s !~ /\s*DISTINCT\s+/i 
     173            distinct = 'DISTINCT '  
     174          end 
     175          sql = "SELECT #{operation}(#{distinct}#{column_name}) AS #{aggregate_alias}" 
    173176 
    174177          # A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT. 
  • trunk/activerecord/test/cases/calculations_test.rb

    r9043 r9223  
    242242  end 
    243243 
     244  def test_should_count_manual_select_with_include 
     245    assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm) 
     246  end 
     247 
    244248  def test_count_with_column_parameter 
    245249    assert_equal 5, Account.count(:firm_id)