# Belongs\_to :2accounts ... :3accounts ... :xaccounts?

**URL:** <https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711>\
**Category:** Rails\
**Created:** [May 5, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711 "2008-05-05T20:17:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 5, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/1 "2008-05-05T20:17:55Z")

</div>

Hello there,

I have two objects as follows:

# BankAccount

int id  
float amount  
Transaction transactions[]

# Transaction

int id  
BankAccount account\_credit\_to # in the db table account\_credit\_to  
is int  
BankAccount account\_debit\_from # int in db table  
float amount  
date\_time

Each transaction is associated with exactly 2 accounts.

How do I map these as Ruby models and access the objects.

Here is my attempt:

def BankAccount  
has\_many :transactions  
end

def Transaction  
#\*\*\* here how do I tell that this transaction references two  
accounts \*\*\* ?  
end

And how do I access them in the controller?

ba = BankAccount.new  
ba.transactions???

Thanks in Advance!

MS

---

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 5, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/2 "2008-05-05T22:07:07Z")

</div>

FWIW, we’ve added another layer (actually several but one it boils  
down to one for the area you’re dealing with). It looks something  
like this:

Account  
has\_many :account\_details  
-opening\_balance  
-reconciled\_balance  
-reconciled\_on

- current\_balance is a method that sums credits, debits and uses them  
to offset the reconciled balance

AccountDetail  
belongs\_to Account  
belongs\_to Transaction

- detail\_type (=Debit or Credit)
- amount

Transaction  
has\_many :account\_details

Beyond the modeling, the main thing you need to do (surprise!) is make  
sure the debits and credits associated with the Transaction balance.

---

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 5, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/3 "2008-05-05T22:30:43Z")

</div>

On 5 May 2008, at 21:06, AndyV wrote:

> - current\_balance is a method that sums credits, debits and uses them
> 
> Beyond the modeling, the main thing you need to do (surprise!) is make  
> sure the debits and credits associated with the Transaction balance.

And also don’t use the word transaction. You’re asking for trouble.

Fred.

---

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 5, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/4 "2008-05-05T22:47:53Z")

</div>

Hey,

to answer your specific question:

class Transaction \< ActiveRecord::Base  
belongs\_to : account\_credit\_to, :class\_name =\> ‘BankAccount’  
belongs\_to :account\_debit\_from, :class\_name =\> ‘BankAccount’  
end

# Note - transactions table must have ‘account\_credit\_to\_id’ and

‘account\_debit\_from’ integer columns.

class BankAccount  
has\_many :credit\_transactions, :as =\> :account\_credit\_to,  
:class\_name =\> ‘Transaction’  
has\_many :debit\_transactions, :as =\> :account\_debit\_from:,  
class\_name =\> ‘Transaction’  
end

HOWEVER… I would strongly advise against this code:

First of all, you might as well treat ‘transaction’ and ‘Transaction’  
as reserved words in Rails. AccountTransaction or something similar  
will help to avoid conflicts.

Next, I don’t know the details of the system you’re trying to create  
but your table definition seems oversimplified for tracking the  
movement of money.

I think you probably should talk to the accountant that’s going to  
eventually have to deal with these records you’re creating. Find out  
the way they want you to record this information in a database (or how  
they’d do it with a paper journal).

Regards,  
Trevor

On 5/5/08, marahddis [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def Transaction  
> Thanks in Advance!
> 
> MS
> 
> >

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 5, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/5 "2008-05-05T23:35:14Z")

</div>

Fred,

yarr - good catch. Fingers clearly moving faster than brain.

Trev

On 5/5/08, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Shouldn’t that be :foreign\_key =\> ‘account\_debit\_from\_id’ (:as creates  
> a polymorphic association which i don’t think is required here)
> 
> Fred

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 6, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/6 "2008-05-06T18:14:49Z")

</div>

Right, we actually use FinancialTransaction.

On May 5, 4:30 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![marahddis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@marahddis](https://www.ruby-forum.com/u/marahddis)\
**Post date:** [May 5, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/belongs-to-2accounts-3accounts-xaccounts/136711/7 "2008-05-05T22:54:21Z")

</div>

On 5 May 2008, at 21:47, Trevor S. wrote:

> # Note - transactions table must have ‘account\_credit\_to\_id’ and
> 
> ‘account\_debit\_from’ integer columns.
> 
> class BankAccount  
> has\_many :credit\_transactions, :as =\> :account\_credit\_to,  
> :class\_name =\> ‘Transaction’  
> has\_many :debit\_transactions, :as =\> :account\_debit\_from:,  
> class\_name =\> ‘Transaction’  
> end

Shouldn’t that be :foreign\_key =\> ‘account\_debit\_from\_id’ (:as creates  
a polymorphic association which i don’t think is required here)

Fred
