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) |
|---|
-
test/cases/calculations_test.rb
old new 241 241 assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) 242 242 end 243 243 244 def test_should_count_manual_select_with_include 245 assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm) 246 end 247 244 248 def test_count_with_column_parameter 245 249 assert_equal 5, Account.count(:firm_id) 246 250 end -
lib/active_record/calculations.rb
old new 169 169 end 170 170 end 171 171 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}" 173 173 174 174 # A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT. 175 175 sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround