# Enumerable sum oddity

**URL:** <https://www.ruby-forum.com/t/enumerable-sum-oddity/89742>\
**Category:** Rails\
**Created:** [March 22, 2007, 1:53pm UTC](https://www.ruby-forum.com/t/enumerable-sum-oddity/89742 "2007-03-22T13:53:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![morten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@morten](https://www.ruby-forum.com/u/morten)\
**Post date:** [March 22, 2007, 1:53pm UTC](https://www.ruby-forum.com/t/enumerable-sum-oddity/89742/1 "2007-03-22T13:53:49Z")

</div>

Hi.

In this setup “account has\_many :consultations”. Can anyone explain  
the following behaviour to me? Rails 1.2.2. The “sum” method should  
return 0 for an empty array.

> > a = Account.find(1)  
> > =\> #\<Account:0x39c80a8 @attributes={…}
> 
> > a.consultations  
> > =\> []
> 
> > a.consultations.sum(&:foo)  
> > ArgumentError: wrong number of arguments (1 for 2)  
> > from ./script/…/config/…/config/…/vendor/rails/activerecord/  
> > lib/active\_record/associations/has\_many\_through\_association.rb:110:in  
> > `calculate' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:110:in`send’  
> > from ./script/…/config/…/config/…/vendor/rails/activerecord/  
> > lib/active\_record/associations/has\_many\_through\_association.rb:110:in  
> > `method_missing' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/base.rb:946:in`with\_scope’  
> > from ./script/…/config/…/config/…/vendor/rails/activerecord/  
> > lib/active\_record/associations/has\_many\_through\_association.rb:110:in  
> > `method_missing' from ./script/../config/../config/../vendor/rails/activerecord/ lib/active_record/associations/has_many_through_association.rb:102:in`sum’  
> > from (irb):15

But if I use a hard coded empty array, things work:

> > [].sum(&:foo)  
> > =\> 0

What’s happening? Thanks.

Morten

---

<div class="post-metadata">

**Author:** ![morten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@morten](https://www.ruby-forum.com/u/morten)\
**Post date:** [March 22, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/enumerable-sum-oddity/89742/2 "2007-03-22T18:41:35Z")

</div>

Hey Morten,

yeah, I feel your pain sharply, because it happened to me too.

It turns out that associations favor the ActiveRecord sum method  
(which issues an sql query) over the Enumerable sum method (which  
iterates over the collection).

Here’s something that’s not fully tested by me but you can stick it  
in your environment.rb and see how it works for you. Once I test it  
better I’ll submit it as a patch.

[http://pastie.caboo.se/48812](http://pastie.caboo.se/48812)

When you don’t pass a block to sum() it’ll end up delegating to the  
AR calculate() method, otherwise it iterates.

so:

collection.sum :id # =\> issues an sql query via calculate()  
collection.sum &:id =\> iterates over the collection due to :id.to\_proc

HTH  
Trevor

---

<div class="post-metadata">

**Author:** ![morten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@morten](https://www.ruby-forum.com/u/morten)\
**Post date:** [March 22, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/enumerable-sum-oddity/89742/3 "2007-03-22T22:04:32Z")

</div>

Thanks Trevor. I also got informed that an explicit .to\_a will work  
(which it did for me):

collection.to\_a.sum(&:id)

Br,

Morten
