From 7d14bd3ff553d6aba11a50b43773bc21ae20f31e Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 19 Jun 2015 10:42:57 -0600 Subject: Use `Enumerable#sum` on `ActiveRecord::Relation` when a block is given This matches our behavior in other cases where useful enumerable methods might have a different definition in `Relation`. Wanting to actually enumerate over the records in this case is completely reasonable, and wanting `.sum` is reasonable for the same reason it is on `Enumerable` in the first place. --- activerecord/test/cases/calculations_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test/cases/calculations_test.rb') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index cb4681109c..aa10817527 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -671,4 +671,14 @@ class CalculationsTest < ActiveRecord::TestCase developer.ratings.includes(comment: :post).where(posts: { id: 1 }).count end end + + def test_sum_uses_enumerable_version_when_block_is_given + block_called = false + relation = Client.all.load + + assert_no_queries do + assert_equal 0, relation.sum { block_called = true; 0 } + end + assert block_called + end end -- cgit v1.2.3