Skip to content

Commit

Permalink
Ensure that calculations properly override the select value even if i…
Browse files Browse the repository at this point in the history
…t's set in the default scope. Fixes rails#1395.
  • Loading branch information
jonleighton committed May 31, 2011
1 parent c22364b commit f4e3b70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/relation_scoping_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit f4e3b70

Please sign in to comment.