I have feeling that there must be one liner or some simple way to do this: ... has_many :items ... def items_value value = 0 items.each { |item| value += items.value } value end Any ideas. Thanks, Igor.
items.inject { |value, item| value + item.value } Chris On 11/4/05, Igor Anic <ianic-eC0b4XhvfLY@public.gmane.org> wrote:> I have feeling that there must be one liner or some simple way to do this: > > ... > has_many :items > ... > def items_value > value = 0 > items.each { |item| value += items.value } > value > end > > > Any ideas. > Thanks, > Igor. > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Actually, that should probably be: items.inject(0) { |value, item| value + item.value } Chris On 11/4/05, Chris McGrath <c.r.mcgrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> items.inject { |value, item| value + item.value } > > Chris > > On 11/4/05, Igor Anic <ianic-eC0b4XhvfLY@public.gmane.org> wrote: > > I have feeling that there must be one liner or some simple way to do this: > > > > ... > > has_many :items > > ... > > def items_value > > value = 0 > > items.each { |item| value += items.value } > > value > > end > > > > > > Any ideas. > > Thanks, > > Igor. > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Check out the count method at http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000694 Warmest regards, Nathan. -------------------------------------------------------------- Nathaniel S. H. Brown Toll Free 1.877.4.INIMIT Inimit Innovations Phone 604.724.6624 www.inimit.com Fax 604.444.9942> -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Igor Anic > Sent: November 4, 2005 2:35 AM > To: Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: [Rails] one liner? > > I have feeling that there must be one liner or some simple > way to do this: > > ... > has_many :items > ... > def items_value > value = 0 > items.each { |item| value += items.value } > value > end > > > Any ideas. > Thanks, > Igor. > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks Chris, that was really fast reply, works great. Chris McGrath wrote:> Actually, that should probably be: > > items.inject(0) { |value, item| value + item.value } > > Chris > > On 11/4/05, Chris McGrath <c.r.mcgrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>items.inject { |value, item| value + item.value } >> >>Chris >> >>On 11/4/05, Igor Anic <ianic-eC0b4XhvfLY@public.gmane.org> wrote: >> >>>I have feeling that there must be one liner or some simple way to do this: >>> >>>... >>> has_many :items >>>... >>> def items_value >>> value = 0 >>> items.each { |item| value += items.value } >>> value >>> end >>> >>> >>>Any ideas. >>>Thanks, >>>Igor. >>> >>> >>> >>> >>> >>>_______________________________________________ >>>Rails mailing list >>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >