# Adding Business Logic in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888>\
**Category:** Rails\
**Created:** [March 23, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888 "2007-03-23T15:37:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [March 23, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888/1 "2007-03-23T15:37:56Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [March 23, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888/2 "2007-03-23T15:56:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [March 23, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888/3 "2007-03-23T16:08:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [March 23, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888/4 "2007-03-23T17:39:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![reg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@reg](https://www.ruby-forum.com/u/reg)\
**Post date:** [March 23, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/adding-business-logic-in-activerecord/89888/5 "2007-03-23T18:59:59Z")

</div>

try

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

model.accepted\_accounttransactions(‘accepted’)
