# Undefined Method error when creating method for collection of objects

**URL:** <https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543>\
**Category:** Rails\
**Created:** [April 2, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543 "2009-04-02T12:57:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 2, 2009, 12:57pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/1 "2009-04-02T12:57:15Z")

</div>

Hi I am learning rails and have hit a bit of a wall.

I have created methods previousley that work fine on a single object.  
However in my latest project I am trying to calculate the total cost  
of a group of phonecalls and I am having difficulty creating a method  
to calculate this. I have a phonecall model and phonecall has a cost  
attribute.

In my controller I am finding the @phonecalls  
And in my view I want to say @phoncalls.total\_cost

However I keep getting an undefined method error.

In my phonecall model i am trying to iterate through a block.

def total\_cost

totalcost=0

each do |phonecall|  
totalcost += phonecall.cost  
end

totalcost  
end

I have tried varios types of blocks, various connertaions of this  
method and self.each… but I dont seem to be getting anywhere.

I am not sure if this is because of the fact that my syntax is wrong  
when dealing with a collection of objects as I am in this case.

I can iterate through those objects in the view ie for each phonecall  
in @phonecalls etc and I am getting the correct output o I am happy  
that the controller code is working fine.

Any help would be muchly appreciated as I am struggling to find help  
in my books and online.

Thank you

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 2, 2009, 1:00pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/2 "2009-04-02T13:00:47Z")

</div>

On 2 Apr 2009, at 11:35, Rick wrote:

> And in my view I want to say @phoncalls.total\_cost
> 
> end

You’ve defined a total\_cost method on the PhoneCall class, but you’re  
trying to call it on an instance of Array (that just happens to  
contain phone calls)  
The easiest way is probably to create a class method that takes as an  
argument the array and returns the total cost.

Fred

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 2, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/3 "2009-04-02T13:13:06Z")

</div>

On 2 Apr 2009, at 11:59, Frederick C. wrote:

> > In my controller I am finding the @phonecalls  
> > And in my view I want to say @phoncalls.total\_cost
> 
> The easiest way is probably to create a class method that takes as an  
> argument the array and returns the total cost.

You could also run in your view e.g. @phonecalls.sum { |p| p.cost }

-Matt

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 2, 2009, 3:08pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/4 "2009-04-02T15:08:31Z")

</div>

Hi Matt, Thanks for your time.

I thought that by putting my total\_cost method in the phonecall model  
that I was doing exactly that.

I appreciate that I can use the view shortcut so thanks for that tip.  
I do need to understand how to create methods that act upon an array  
of objects both in this instance and obviousley for my future  
understanding of this subject.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 2, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/5 "2009-04-02T15:03:15Z")

</div>

On Apr 2, 11:59 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > attribute.  
> > argument the array and returns the total cost.
> 
> Fred

Hi Fred thnaks for your time.

I thought that I had created a class method in the (phonecall class) .  
Do you mean in the array class? And how do I do that. Is it not  
possibl;e to create methods in the phonecall model file that act upon  
a collection(array) of that object or do they have to be acting only  
on a single instance of that object.

Once again thanks for your time.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 3, 2009, 1:38am UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/6 "2009-04-03T01:38:40Z")

</div>

Hi Frederick,

Once again thankyou for taking the time to respond.

I am a little confused by your response.

Your method seems to suggest that I pass an argument/array into the  
method.

@phonecalls should already contain all of the array data, no? Why do I  
need to pass this in. Cant I just create a method that acts upon the  
multiple objects of @phonecalls ?

Thanks

Richard

On Apr 2, 4:14 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 2, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/7 "2009-04-02T17:15:07Z")

</div>

On Apr 2, 2:00 pm, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Apr 2, 11:59 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> Hi Fred thnaks for your time.
> 
> I thought that I had created a class method in the (phonecall class) .  
> Do you mean in the array class? And how do I do that. Is it not  
> No. (well technically you could create methods on the Array class but  
> that would be messy).

you’d want something like

def self.total\_cost(array\_of\_phone\_calls)  
…  
end

and then PhoneCall.total\_cost(some\_array)

Fred

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [April 3, 2009, 9:57am UTC](https://www.ruby-forum.com/t/undefined-method-error-when-creating-method-for-collection-of-objects/164543/8 "2009-04-03T09:57:43Z")

</div>

On Apr 3, 12:37 am, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> need to pass this in. Cant I just create a method that acts upon the  
> multiple objects of @phonecalls ?

@phonecalls is an Array, so to be able to do @phonecalls.total\_cost  
you would have to add a total\_cost method to Array. If you start doing  
this willy nilly, Array will very quickly have dozens of methods that  
only apply if the array contains elements of some particular class  
that sounds really horrible. (the are alternatives, like extending AR  
to return a result set proxy instead of an array etc. but that’s  
rather out of scope)

Fred
