diff options
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 5 | ||||
-rw-r--r-- | activerecord/test/calculations_test.rb | 4 |
3 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 23845ebaf0..ccce8c44c5 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add AssociationCollection#sum since the method_missing invokation has been shadowed by Enumerable#sum. + * Added find_or_initialize_by_X which works like find_or_create_by_X but doesn't save the newly instantiated record. [Sam Stephenson] * Row locking. Provide a locking clause with the :lock finder option or true for the default "FOR UPDATE". Use the #lock! method to obtain a row lock on a single record (reloads the record with :lock => true). [Shugo Maeda] diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 61ad6ab7b6..ec7af7913d 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -42,6 +42,11 @@ module ActiveRecord reset_target! end + # Calculate sum using SQL, not Enumerable + def sum(*args, &block) + calculate(:sum, *args, &block) + end + # Remove +records+ from this association. Does not destroy +records+. def delete(*records) records = flatten_deeper(records) diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb index 67de1b9bd5..7b89e8c62c 100644 --- a/activerecord/test/calculations_test.rb +++ b/activerecord/test/calculations_test.rb @@ -147,6 +147,10 @@ class CalculationsTest < Test::Unit::TestCase assert_equal 3, c['CLIENT'] assert_equal 2, c['FIRM'] end + + def test_should_not_overshadow_enumerable_sum + assert_equal 6, [1, 2, 3].sum(&:abs) + end def test_should_sum_scoped_field assert_equal 15, companies(:rails_core).companies.sum(:id) |