Skip to content
Browse files

Call sum on the scope directly, rather than relying on method_missing…

… and calculate
  • Loading branch information...
1 parent ca77858 commit 140b269fb7aee85e6da32da5305f771099d35af5 @jonleighton jonleighton committed Jan 24, 2011
Showing with 2 additions and 2 deletions.
  1. +2 −2 activerecord/lib/active_record/associations/association_collection.rb
View
4 activerecord/lib/active_record/associations/association_collection.rb
@@ -125,9 +125,9 @@ def destroy_all
# Calculate sum using SQL, not Enumerable
def sum(*args)
if block_given?
- calculate(:sum, *args) { |*block_args| yield(*block_args) }
+ scoped.sum(*args) { |*block_args| yield(*block_args) }
@pixeltrix
Ruby on Rails member
pixeltrix added a note Aug 24, 2012

This line should've been:

load_target.sum(*args) { |*block_args| yield(*block_args) }

as ActiveRecord::Relation#sum will call to_a on the scope anyway so we don't save on loading the records and we have the additional overhead of creating the scope. Also it bypasses association specific behaviour like setting inverse instances on associations. :cry:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
else
- calculate(:sum, *args)
+ scoped.sum(*args)
end
end

0 comments on commit 140b269

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