# How to handle cardinality 1-to-2 with Associations?

**URL:** <https://www.ruby-forum.com/t/how-to-handle-cardinality-1-to-2-with-associations/108173>\
**Category:** Rails\
**Created:** [June 29, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/how-to-handle-cardinality-1-to-2-with-associations/108173 "2007-06-29T17:52:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [June 29, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/how-to-handle-cardinality-1-to-2-with-associations/108173/1 "2007-06-29T17:52:47Z")

</div>

Hello,

I am new to RoR and am coding a small budgetting app with two tables  
(see below). As you can see every account has\_many transactions and  
every transaction has 2 accounts. How can I best respresent this using  
ActiveRecord::Associations ?

create table accounts (  
id int not null auto\_increment,  
name varchar(100) not null default ‘’,  
primary key(id)  
);

create table transactions (  
id int not null  
auto\_increment,  
month varchar(3) not null,  
amount int null,  
from\_id int not null,  
to\_id int not null,  
constraint fk\_from\_accounts foreign key (from\_id) references  
accounts(id),  
constraint fk\_to\_accounts foreign key (to\_id) references  
accounts(id),  
primary key(id)  
) engine=InnoDB;

I see 3 possible solutions but nothing elegant:

- create 2 relationship tables (from and to)
- use sql
- code belongs\_to\_2 association

I would very much like your advice on this.

Thanks !  
Simon

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [June 29, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/how-to-handle-cardinality-1-to-2-with-associations/108173/2 "2007-06-29T20:21:01Z")

</div>

That certainly works. You can also put in a validation for the  
transaction and the account classes to limit the number of  
associations between them if you want to treat all associations  
uniformly.

Michael

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [June 29, 2007, 6:12pm UTC](https://www.ruby-forum.com/t/how-to-handle-cardinality-1-to-2-with-associations/108173/3 "2007-06-29T18:12:49Z")

</div>

Hi Simon,

I think you need to model two relationships.

class Transaction \< AR::Base  
belongs\_to :creditor, :class\_name =\> ‘Account’, :foreign\_key =\>  
‘creditor\_id’ #from\_id  
belongs\_to :debitor, :class\_name =\> ‘Account’, :foreign\_key =\>  
‘debitor\_id’ #to\_id  
end

class Account \< AR:Base  
has\_many :credits, :class\_name =\> ‘Transaction’, :foreign\_key =\>  
‘creditor\_id’ #from\_id  
has\_many :debits, : class\_name =\> ‘Transaction’, :foreign\_key =\>  
‘debitor\_id’ #to\_id  
end

Steve

---

<div class="post-metadata">

**Author:** ![simon1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@simon1](https://www.ruby-forum.com/u/simon1)\
**Post date:** [June 30, 2007, 8:27am UTC](https://www.ruby-forum.com/t/how-to-handle-cardinality-1-to-2-with-associations/108173/4 "2007-06-30T08:27:59Z")

</div>

Thanks you Steve, that is exactly what I was looking for. (I got so  
close but couldn’t figure it out 🙂 ).

Simon
