Adding Business Logic in ActiveRecord


#1

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


#2

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


#3

Chris H. 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


#4

Reg Phipps wrote:

Chris H. 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


#5

try

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

model.accepted_accounttransactions(‘accepted’)