# Has\_many association where the source model can be in several columns

**URL:** <https://www.ruby-forum.com/t/has-many-association-where-the-source-model-can-be-in-several-columns/128169>\
**Category:** Rails\
**Created:** [February 5, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/has-many-association-where-the-source-model-can-be-in-several-columns/128169 "2008-02-05T15:10:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![njnjnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@njnjnj](https://www.ruby-forum.com/u/njnjnj)\
**Post date:** [February 5, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/has-many-association-where-the-source-model-can-be-in-several-columns/128169/1 "2008-02-05T15:10:08Z")

</div>

Hello all,

I have an application dealing with User and Transaction objects, each  
transaction going from a source user to a target user.

I have defined the following associations in user.rb :

has\_many :transactions\_as\_source, :class\_name =\> “Transaction”, :as  
=\> :source  
has\_many :transactions\_as\_target, :class\_name =\> “Transaction”, :as  
=\> :target

and this is working fine, but I’m looking for a way to define  
“has\_many :transactions” to get all the transactions having the user  
as a source _OR_ a target, i.e. something like:

has\_many :transactions, :finder\_sql =\> ‘SELECT \* FROM transactions  
WHERE source\_id = #{id} OR target\_id = #{id}’

but without resorting to finder\_sql (as with this definition I cannot  
cascade additional queries such as  
current\_user.transactions.sum(:amount) or  
current\_user.transactions.find(…).

Thanks,

Nicolas

---

<div class="post-metadata">

**Author:** ![njnjnj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@njnjnj](https://www.ruby-forum.com/u/njnjnj)\
**Post date:** [February 26, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/has-many-association-where-the-source-model-can-be-in-several-columns/128169/2 "2008-02-26T21:52:08Z")

</div>

Hum, it looks like you are about to enjoy the has\_many through  
polymorphism troubles…

Check here : [has\_many :through - The other side of polymorphic :through associations](http://blog.hasmanythrough.com/2006/4/3/polymorphic-through)

My only personal advice will be : be VERY careful with each letter  
(specifically the “s”) you will use because if it does not work Rails  
won’t say a word.

H

nj wrote:

> Hello all,
> 
> I have an application dealing with User and Transaction objects, each  
> transaction going from a source user to a target user.
> 
> I have defined the following associations in user.rb :
> 
> has\_many :transactions\_as\_source, :class\_name =\> “Transaction”, :as  
> =\> :source  
> has\_many :transactions\_as\_target, :class\_name =\> “Transaction”, :as  
> =\> :target
> 
> and this is working fine, but I’m looking for a way to define  
> “has\_many :transactions” to get all the transactions having the user  
> as a source _OR_ a target, i.e. something like:
> 
> has\_many :transactions, :finder\_sql =\> ‘SELECT \* FROM transactions  
> WHERE source\_id = #{id} OR target\_id = #{id}’
> 
> but without resorting to finder\_sql (as with this definition I cannot  
> cascade additional queries such as  
> current\_user.transactions.sum(:amount) or  
> current\_user.transactions.find(…).
> 
> Thanks,
> 
> Nicolas
