# has\_many / polymorphic

**URL:** <https://discuss.rubyonrails.org/t/has-many-polymorphic/29782>\
**Category:** rubyonrails-talk\
**Created:** [May 25, 2008, 5:39am UTC](https://discuss.rubyonrails.org/t/has-many-polymorphic/29782 "2008-05-25T05:39:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![a111](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@a111](https://discuss.rubyonrails.org/u/a111)\
**Post date:** [May 25, 2008, 5:39am UTC](https://discuss.rubyonrails.org/t/has-many-polymorphic/29782/1 "2008-05-25T05:39:50Z")

</div>

hi all -

i've got roughly the following from a legacy db (so no suggestions of  
changing schema)

a class which models a peice a web content

&nbsp;&nbsp;&nbsp;class WebContent &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# web\_contentid | content &nbsp;&nbsp;&nbsp;end

a join table which maps to various associated things, here type is  
almost exactly the same as is used for rails sti/polymorphic associations.  
details is the foreign key pointing to that type.

&nbsp;&nbsp;&nbsp;class WebContentAssociation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# web\_contentid | type | details &nbsp;&nbsp;&nbsp;end

also there are various models, of course, which can be related to a  
peice of web content.

&nbsp;&nbsp;&nbsp;class Country &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# countrycode | ... &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class School &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# schoolcode | ... &nbsp;&nbsp;&nbsp;end

so a sample WebContentAssociation row might be

&nbsp;&nbsp;&nbsp;42 | 'country' | 'usa'

aka web content # 42 is associated with country 'usa'

the issue i'm having is this: it seems like a good case for polymorphic associations and, in the case of belongs to associations this is true,  
but for has\_many associations i can't quite figure out if i'm missing  
something, or if rails is missing something to make the 'right thing' happen with these relationships. so for example, i'd like to be able to say

&nbsp;&nbsp;&nbsp;web\_content.countries

but the issue, i think, is not being able to specify the type/ foreign\_key columns for has\_many associations in the same way that you can for  
polymorphic belongs to associations.

so while i \*can\* do something like this

&nbsp;&nbsp;&nbsp;class WebContent &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :web\_content\_associations, :foreign\_key =\> "web\_contentid" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :countries, :through =\> :web\_content\_associations &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class WebContentAssociation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :web\_content, :foreign\_key =\> "web\_contentid" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :country, :foreign\_key =\> "details", :conditions =\>  
"type='country'" &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class Country &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :web\_content\_associations, :foreign\_key =\>  
"details", :conditions =\> "type='country'", :dependent =\> :destroy &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :web\_contents, :through =\> :web\_content\_associations &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;p web\_content.countries

i cannnot do something like

&nbsp;&nbsp;&nbsp;class WebContent &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :web\_content\_associations, :foreign\_key =\> "web\_contentid" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :associations, :through =\> :web\_content\_associations &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class WebContentAssociation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :web\_content, :foreign\_key =\> "web\_contentid" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;belongs\_to :association, :polymorphic =\> true, :foreign\_type =\>  
"type", :foreign\_key =\> "details" &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class Country &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :web\_content\_associations, :as =\> :association ###  
of course this breaks down... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;has\_many :web\_contents, :through =\> :web\_content\_associations &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;p web\_content.countries

as far as i can tell this is because

&nbsp;&nbsp;&nbsp;has\_many :web\_content\_associations, :as =\> :associations

pins the sql to

&nbsp;&nbsp;&nbsp;'where association\_id=xx and association\_type=yy'

instead of the

&nbsp;&nbsp;&nbsp;'where details=xx and type=yy'

which is what i need.

in otherwords i need to configure

&nbsp;&nbsp;&nbsp;has\_many :foos, :as =\> :bars

to \*not\* use

&nbsp;&nbsp;&nbsp;'bars\_type' and 'bars\_id'

but two other fields.

anyone know if this is possible?

cheers.

note : the application is using the composite primary key plugin, so Model.find(id) is a bit perverted from the 'normal' ar way...

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 25, 2008, 11:30am UTC](https://discuss.rubyonrails.org/t/has-many-polymorphic/29782/2 "2008-05-25T11:30:26Z")

</div>

> in otherwords i need to configure
> 
> &nbsp;&nbsp;has\_many :foos, :as =\> :bars
> 
> to \*not\* use
> 
> &nbsp;&nbsp;'bars\_type' and 'bars\_id'
> 
> but two other fields.

Looking at the source it's hardwired to look at bars\_id and bars\_type.  
You should be able to override all this with the :finder\_sql option,  
but that does of course require that you specify the whole big chunk  
of sql needed.

Fred

---

<div class="post-metadata">

**Author:** ![a111](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@a111](https://discuss.rubyonrails.org/u/a111)\
**Post date:** [May 25, 2008, 2:47pm UTC](https://discuss.rubyonrails.org/t/has-many-polymorphic/29782/3 "2008-05-25T14:47:22Z")

</div>

yeah, i read that but was hoping someone would have a clever workaround. oh well!

cheers.
