diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-05-31 21:09:25 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-05-31 21:12:20 +0100 |
commit | fbe012e92d7792e1307b6ccaab933ea78f635a0b (patch) | |
tree | 6cd5044d9f2e2aae82d1d26bc5c6ca60cd57f84f /activerecord | |
parent | a5806fa409ff286e86eb9fabcee25d083892606f (diff) | |
download | rails-fbe012e92d7792e1307b6ccaab933ea78f635a0b.tar.gz rails-fbe012e92d7792e1307b6ccaab933ea78f635a0b.tar.bz2 rails-fbe012e92d7792e1307b6ccaab933ea78f635a0b.zip |
Ensure that calculations properly override the select value even if it's set in the default scope. Fixes #1395.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/relation_scoping_test.rb | 5 |
2 files changed, 7 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index a785f38e89..aabe5c269b 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -196,7 +196,7 @@ module ActiveRecord 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 @@ module ActiveRecord "#{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) diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index 0997a81a8a..83bc472764 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -475,6 +475,11 @@ class DefaultScopingTest < ActiveRecord::TestCase 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 |