I have some business logic in a class class Account < ActiveRecord::Base ... .... def received_accounttransactions accounttransactions.inject(0) {|total, accounttransaction| total + accounttransaction.amount } end end and a helper to display the total as in the article http://developer.apple.com/tools/rubyonrails.html. This works but I now want to filter the total so that an amount is only added on a condition that the status column is ''active''. I''ve tried to using def accepted_accounttransactions accounttransactions.inject(0) {|total, accounttransaction| total + accounttransaction.amount (:condition => accounttransaction.status =''accepted'') } end but I get an error. I tried to remove the () around the filter but get a wrong number of arguments. What should I be doing to filter on this business logic? Thanks Reg -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
not sure where you got the :condition from but try this def accepted_accounttransactions accounttransactions.inject(0) {|total, at| total + (at.status =''accepted'' ? at.amount : 0 } end On 3/23/07, Reg Phipps <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have some business logic in a class > > class Account < ActiveRecord::Base > > ... > .... > > def received_accounttransactions > accounttransactions.inject(0) {|total, accounttransaction| total + > accounttransaction.amount } > end > end > > > and a helper to display the total as in the article > http://developer.apple.com/tools/rubyonrails.html. This works but I now > want to filter the total so that an amount is only added on a condition > that the status column is ''active''. I''ve tried to using > > > def accepted_accounttransactions > accounttransactions.inject(0) {|total, accounttransaction| total + > accounttransaction.amount (:condition => accounttransaction.status => ''accepted'') } > end > > but I get an error. I tried to remove the () around the filter but get a > wrong number of arguments. What should I be doing to filter on this > business logic? > > > Thanks > Reg > > -- > Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Chris Hall wrote:> not sure where you got the :condition from but try this > > def accepted_accounttransactions > accounttransactions.inject(0) {|total, at| total + (at.status => ''accepted'' ? at.amount : 0 } > endThanks Chris, that worked a treat. Reg -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Reg Phipps wrote:> Chris Hall wrote: >> not sure where you got the :condition from but try this >> >> def accepted_accounttransactions >> accounttransactions.inject(0) {|total, at| total + (at.status =>> ''accepted'' ? at.amount : 0 } >> end > > Thanks Chris, that worked a treat. > > RegWhat would I use if I wanted to pass the filter argument i.e. ''accepted'' in Thanks Reg -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
try def accepted_accounttransactions(filter) accounttransactions.inject(0) {|total, at| total + (at.status =filter ? at.amount : 0 } end model.accepted_accounttransactions(''accepted'') On 3/23/07, Reg Phipps <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Reg Phipps wrote: > > Chris Hall wrote: > >> not sure where you got the :condition from but try this > >> > >> def accepted_accounttransactions > >> accounttransactions.inject(0) {|total, at| total + (at.status => >> ''accepted'' ? at.amount : 0 } > >> end > > > > Thanks Chris, that worked a treat. > > > > Reg > What would I use if I wanted to pass the filter argument i.e. ''accepted'' > in > > Thanks > Reg > > -- > Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---