Hi, I have a method like this, calculating the sub_total in an order model: def sub_total result = 0 for item in self.order_items result += item.price end result end Is there a rails way to make the method more simple and concise? Thank you :) -- regards, Kai Fu Material Science and Engineering Huazhong University of Science and Technology Wuhan Hubei Province P.R.China E-mail: corntrace-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-Jun-05 07:20 UTC
Re: How to add up an array''s element with a rails way?
On Jun 5, 5:01 am, Kevin Fu <corntr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a method like this, calculating the sub_total in an order model: > > def sub_total > result = 0 > for item in self.order_items > result += item.price > end > result > end > > Is there a rails way to make the method more simple and concise? >Have a peak at the sum method :-) Fred> Thank you :) > > -- > regards, > Kai Fu > > Material Science and Engineering > Huazhong University of Science and Technology > Wuhan Hubei Province P.R.China > E-mail: corntr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Sandip Ransing
2009-Jun-05 07:52 UTC
Re: How to add up an array''s element with a rails way?
use active record sum method self.order_items.sum( :price ) -Sandip On Fri, Jun 5, 2009 at 12:50 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Jun 5, 5:01 am, Kevin Fu <corntr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, > > > > I have a method like this, calculating the sub_total in an order model: > > > > def sub_total > > result = 0 > > for item in self.order_items > > result += item.price > > end > > result > > end > > > > Is there a rails way to make the method more simple and concise? > > > > Have a peak at the sum method :-) > > Fred > > Thank you :) > > > > -- > > regards, > > Kai Fu > > > > Material Science and Engineering > > Huazhong University of Science and Technology > > Wuhan Hubei Province P.R.China > > E-mail: corntr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > >-- Ruby on Rails Developer http://sandip.sosblog.com http://funonrails.wordpress.com www.joshsoftware.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---