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

Ticket #11502: fix_for_count_with_select_distinct_and_eager_loading.diff

File fix_for_count_with_select_distinct_and_eager_loading.diff, 1.3 kB (added by kenneth.kalmer, 8 months ago)

Patch against r9205

  • test/cases/calculations_test.rb

    old new  
    241241    assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) 
    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) 
    246250  end 
  • lib/active_record/calculations.rb

    old new  
    169169            end 
    170170          end 
    171171 
    172           sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}" 
     172          sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct] && column_name.to_s.index(/\s*DISTINCT\s+/i).nil?}#{column_name}) AS #{aggregate_alias}" 
    173173 
    174174          # A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT. 
    175175          sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround