Rick
2009-Apr-02 10:35 UTC
Undefined Method error when creating method for collection of objects
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-02 10:59 UTC
Re: Undefined Method error when creating method for collection of objects
On 2 Apr 2009, at 11:35, Rick wrote:> > 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 > > > endYou''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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew MacLeod
2009-Apr-02 11:12 UTC
Re: Undefined Method error when creating method for collection of objects
On 2 Apr 2009, at 11:59, Frederick Cheung 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick
2009-Apr-02 13:00 UTC
Re: Undefined Method error when creating method for collection of objects
On Apr 2, 11:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2 Apr 2009, at 11:35, Rick wrote: > > > > > 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 > > > 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. > > FredHi 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick
2009-Apr-02 13:04 UTC
Re: Undefined Method error when creating method for collection of objects
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. On Apr 2, 12:12 pm, Matthew MacLeod <m...-UN/XyDXeKd87VdE/fOJbLw@public.gmane.org> wrote:> On 2 Apr 2009, at 11:59, Frederick Cheung 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-02 15:14 UTC
Re: Undefined Method error when creating method for collection of objects
On Apr 2, 2:00 pm, Rick <rfw...-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote:> On Apr 2, 11:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 notNo. (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> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick
2009-Apr-02 23:37 UTC
Re: Undefined Method error when creating method for collection of objects
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 2, 2:00 pm, Rick <rfw...-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote: > > > On Apr 2, 11:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > 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 > > > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-03 07:56 UTC
Re: Undefined Method error when creating method for collection of objects
On Apr 3, 12:37 am, Rick <rfw...-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote:> 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 ? >@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> Thanks > > Richard > > On Apr 2, 4:14 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Apr 2, 2:00 pm, Rick <rfw...-FhtRXb7CoQBt1OO0OYaSVA@public.gmane.org> wrote: > > > > On Apr 2, 11:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > 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 > > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---