aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-06-23 19:57:49 +0100
committerJon Leighton <j@jonathanleighton.com>2011-06-23 19:57:49 +0100
commit87d6865bf71eb3feb5831ca541f3493aa36ee88e (patch)
treef08c74bfb942427e621ee898b2c4b1c3a3fa51c9 /activerecord/lib/active_record/relation
parent8a1319dec09672cbb1a4d6fce1db09f10c44bceb (diff)
downloadrails-87d6865bf71eb3feb5831ca541f3493aa36ee88e.tar.gz
rails-87d6865bf71eb3feb5831ca541f3493aa36ee88e.tar.bz2
rails-87d6865bf71eb3feb5831ca541f3493aa36ee88e.zip
Apply the default scope earlier when doing calculations. Fixes #1682.
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb16
1 files changed, 11 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index aabe5c269b..9c5f603c01 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -146,10 +146,16 @@ module ActiveRecord
if options.except(:distinct).present?
apply_finder_options(options.except(:distinct)).calculate(operation, column_name, :distinct => options[:distinct])
else
- if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
- construct_relation_for_association_calculations.calculate(operation, column_name, options)
+ relation = with_default_scope
+
+ if relation.equal?(self)
+ if eager_loading? || (includes_values.present? && references_eager_loaded_tables?)
+ construct_relation_for_association_calculations.calculate(operation, column_name, options)
+ else
+ perform_calculation(operation, column_name, options)
+ end
else
- perform_calculation(operation, column_name, options)
+ relation.calculate(operation, column_name, options)
end
end
rescue ThrowResult
@@ -196,7 +202,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 = with_default_scope.reorder(nil)
+ relation = reorder(nil)
if operation == "count" && (relation.limit_value || relation.offset_value)
# Shortcut when limit is zero.
@@ -245,7 +251,7 @@ module ActiveRecord
"#{field} AS #{aliaz}"
}
- relation = with_default_scope.except(:group).group(group.join(','))
+ relation = except(:group).group(group.join(','))
relation.select_values = select_values
calculated_data = @klass.connection.select_all(relation.to_sql)