Skip to content
This repository
Browse code

Ensure that calculations properly override the select value even if i…

…t's set in the default scope. Fixes #1395.
  • Loading branch information...
commit f4e3b70e9cbf31f9ba11a0b165aff7aeb01f2e70 1 parent c22364b
Jon Leighton authored May 31, 2011
4  activerecord/lib/active_record/relation/calculations.rb
@@ -196,7 +196,7 @@ def operation_over_aggregate_column(column, operation, distinct)
196 196
 
197 197
     def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
198 198
       # Postgresql doesn't like ORDER BY when there are no GROUP BY
199  
-      relation = reorder(nil)
  199
+      relation = with_default_scope.reorder(nil)
200 200
 
201 201
       if operation == "count" && (relation.limit_value || relation.offset_value)
202 202
         # Shortcut when limit is zero.
@@ -245,7 +245,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
245 245
         "#{field} AS #{aliaz}"
246 246
       }
247 247
 
248  
-      relation = except(:group).group(group.join(','))
  248
+      relation = with_default_scope.except(:group).group(group.join(','))
249 249
       relation.select_values = select_values
250 250
 
251 251
       calculated_data = @klass.connection.select_all(relation.to_sql)
5  activerecord/test/cases/relation_scoping_test.rb
@@ -475,6 +475,11 @@ def test_default_scope_select_ignored_by_aggregations
475 475
     assert_equal DeveloperWithSelect.all.count, DeveloperWithSelect.count
476 476
   end
477 477
 
  478
+  def test_default_scope_select_ignored_by_grouped_aggregations
  479
+    assert_equal Hash[Developer.all.group_by(&:salary).map { |s, d| [s, d.count] }],
  480
+                 DeveloperWithSelect.group(:salary).count
  481
+  end
  482
+
478 483
   def test_default_scope_order_ignored_by_aggregations
479 484
     assert_equal DeveloperOrderedBySalary.all.count, DeveloperOrderedBySalary.count
480 485
   end

0 notes on commit f4e3b70

Please sign in to comment.
Something went wrong with that request. Please try again.