Skip to content
Browse files

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

…t's set in the default scope. Fixes #1395.
  • Loading branch information...
1 parent c22364b commit f4e3b70e9cbf31f9ba11a0b165aff7aeb01f2e70 @jonleighton jonleighton committed May 31, 2011
View
4 activerecord/lib/active_record/relation/calculations.rb
@@ -196,7 +196,7 @@ def operation_over_aggregate_column(column, operation, distinct)
def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
# Postgresql doesn't like ORDER BY when there are no GROUP BY
- relation = reorder(nil)
+ relation = with_default_scope.reorder(nil)
if operation == "count" && (relation.limit_value || relation.offset_value)
# Shortcut when limit is zero.
@@ -245,7 +245,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
"#{field} AS #{aliaz}"
}
- relation = except(:group).group(group.join(','))
+ relation = with_default_scope.except(:group).group(group.join(','))
relation.select_values = select_values
calculated_data = @klass.connection.select_all(relation.to_sql)
View
5 activerecord/test/cases/relation_scoping_test.rb
@@ -475,6 +475,11 @@ def test_default_scope_select_ignored_by_aggregations
assert_equal DeveloperWithSelect.all.count, DeveloperWithSelect.count
end
+ def test_default_scope_select_ignored_by_grouped_aggregations
+ assert_equal Hash[Developer.all.group_by(&:salary).map { |s, d| [s, d.count] }],
+ DeveloperWithSelect.group(:salary).count
+ end
+
def test_default_scope_order_ignored_by_aggregations
assert_equal DeveloperOrderedBySalary.all.count, DeveloperOrderedBySalary.count
end

0 comments on commit f4e3b70

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