# Strange Array behaviour

**URL:** <https://www.ruby-forum.com/t/strange-array-behaviour/121651>\
**Category:** Rails\
**Created:** [November 29, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/121651 "2007-11-29T17:35:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [November 29, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/121651/1 "2007-11-29T17:35:57Z")

</div>

I’m having a strange thing when using the Enumerable extensions for  
Rails:  
Have a look at the following code:

class Invoice \< ActiveRecord::Base  
has\_many :invoice\_lines

def total  
invoice\_lines.sum(&:total)  
end  
end

class InvoiceLine \< ActiveRecord::Base  
belongs\_to :invoice

def total  
amount \* price  
end  
end

Now, when calling invoice.total, I get wrong number of arguments (1 for  
2) error, but when changing the the total method in Invoice to:

def total  
invoice\_lines.to\_a.sum(&:total)  
end

I’m not sure what causes the extra ‘to\_a’ to get the thing to work,  
since invoice\_lines.class also returns Array.

Anyone?

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [November 29, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/strange-array-behaviour/121651/2 "2007-11-29T17:45:06Z")

</div>

On 29 Nov 2007, at 16:35, Wouter de Bie wrote:

> I’m not sure what causes the extra ‘to\_a’ to get the thing to work,  
> since invoice\_lines.class also returns Array.

invoice\_lines.class returns Array, but invoice\_lines is not an Array,  
but an association proxy.

This association proxy defines a sum (and count etc…) method that is  
analogous to the one available on any model.  
So in the same way that you would do Invoice.sum(:column\_name), you  
need to do invoice\_lines.sum(:total)

The reason the to\_a makes a difference is that it forces the proxy  
class to give you the real array.

Fred

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [November 30, 2007, 10:35am UTC](https://www.ruby-forum.com/t/strange-array-behaviour/121651/3 "2007-11-30T10:35:26Z")

</div>

Frederick C. wrote:

> On 29 Nov 2007, at 16:35, Wouter de Bie wrote:
> 
> > I’m not sure what causes the extra ‘to\_a’ to get the thing to work,  
> > since invoice\_lines.class also returns Array.
> 
> invoice\_lines.class returns Array, but invoice\_lines is not an Array,  
> but an association proxy.

Aha! I see! What I don’t really understand is that invoice\_lines.class  
is an Array and not something like an ArrayAssociationProxy (or  
something along those lines), since (imho) one of Ruby’s awesome  
features is duck typing… In this case, it looks like a duck but doesn’t  
quack like one.

Wouter

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [November 30, 2007, 10:38am UTC](https://www.ruby-forum.com/t/strange-array-behaviour/121651/4 "2007-11-30T10:38:26Z")

</div>

Another thing I noticed is that sum() on the object requires the name of  
a column and not an attribute of the object. I have a case where I want  
the sum of a calculated attribute, so I need the to\_a in between. I’m  
not sure if this is nice behavior.
