# Two has\_many association for a single model

**URL:** <https://www.ruby-forum.com/t/two-has-many-association-for-a-single-model/158575>\
**Category:** Rails\
**Created:** [January 25, 2009, 5:09am UTC](https://www.ruby-forum.com/t/two-has-many-association-for-a-single-model/158575 "2009-01-25T05:09:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachinkale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@sachinkale](https://www.ruby-forum.com/u/sachinkale)\
**Post date:** [January 25, 2009, 5:09am UTC](https://www.ruby-forum.com/t/two-has-many-association-for-a-single-model/158575/1 "2009-01-25T05:09:49Z")

</div>

Hi

I have following models

1. employee

2. transaction

Requirement - A transaction has a dealer(employee) and a  
driver(employee).

## I experimented following

I have put -

/models/employee.rb :

has\_many :transactions, :as =\> :dealer  
has\_many :transactions, :as =\> :driver

/models/transaction.rb :

belongs\_to :dealer, :class =\> :employee, :foreign\_key =\> :employee\_id  
belongs\_to :driver, :class =\> :employee, :foreign\_key =\> :employee\_id

## Transaction table has columns - dealer\_id, driver\_id,

Now the problem is how can I get something like @transaction.dealer.name  
or  
employee.transactions

* * *

I certainly know that my experiment has basic flaws of understanding  
associations.

Please help me!

---

<div class="post-metadata">

**Author:** ![sachinkale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@sachinkale](https://www.ruby-forum.com/u/sachinkale)\
**Post date:** [January 25, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/two-has-many-association-for-a-single-model/158575/2 "2009-01-25T15:51:33Z")

</div>

Thanks, that was a great help. Also tip of ‘transaction’ naming was very  
useful one. Thanks again!

---

<div class="post-metadata">

**Author:** ![sachinkale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@sachinkale](https://www.ruby-forum.com/u/sachinkale)\
**Post date:** [January 25, 2009, 9:00am UTC](https://www.ruby-forum.com/t/two-has-many-association-for-a-single-model/158575/3 "2009-01-25T09:00:33Z")

</div>

On 25 Jan 2009, at 04:09, Sachin K. wrote:

> driver(employee).
> 
> ## I experimented following
> 
> I have put -
> 
> /models/employee.rb :
> 
> has\_many :transactions, :as =\> :dealer  
> has\_many :transactions, :as =\> :driver

you’re misusing :as here - that’s for something completely different

> /models/transaction.rb :
> 
> belongs\_to :dealer, :class =\> :employee, :foreign\_key =\> :employee\_id  
> belongs\_to :driver, :class =\> :employee, :foreign\_key =\> :employee\_id
> 
> Transaction table has columns - dealer\_id, driver\_id,

If that’s the case then the :foreign\_key option should be ‘dealer\_id’  
and ‘driver\_id’ (and thus unnecessary as that’s the default).  
The :class option is also supposed to be :class\_name  
I’ve got an example of something similar here:

> **[Creating multiple associations with the same table - Space Vatican](https://www.spacevatican.org/2008/5/6/creating-multiple-associations-with-the-same-table/)**
>
> Rails makes setting up your associations dead easy and very concise. There’s a whole bunch of options that can be set however as long as you …

Lastly, be wary of associations called transaction before Rails 2.3 -  
the transaction method created by the association will stomp on an  
internal method

Fred
