Forum: Ruby on Rails Adding Business Logic in ActiveRecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
05462f33d62930f490d5f6c38753b81f?d=identicon&s=25 Reg Phipps (rjp)
on 2007-03-23 15:37
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-23 15:56
(Received via mailing list)
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
05462f33d62930f490d5f6c38753b81f?d=identicon&s=25 Reg Phipps (rjp)
on 2007-03-23 16:08
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
05462f33d62930f490d5f6c38753b81f?d=identicon&s=25 Reg Phipps (rjp)
on 2007-03-23 17:39
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-03-23 18:59
(Received via mailing list)
try

def accepted_accounttransactions(filter)
  accounttransactions.inject(0) {|total, at| total + (at.status ==
filter ? at.amount : 0 }
end

model.accepted_accounttransactions('accepted')
This topic is locked and can not be replied to.