# Has\_many / polymorphic

**URL:** <https://www.ruby-forum.com/t/has-many-polymorphic/138595>\
**Category:** Rails\
**Created:** [May 25, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/has-many-polymorphic/138595 "2008-05-25T13:17:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [May 25, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/has-many-polymorphic/138595/1 "2008-05-25T13:17:51Z")

</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

class WebContent  
# web\_contentid | content  
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.

class WebContentAssociation  
# web\_contentid | type | details  
end

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

class Country  
# countrycode | …  
end

class School  
# schoolcode | …  
end

so a sample WebContentAssociation row might be

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

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

class WebContent  
has\_many :web\_content\_associations, :foreign\_key =\> “web\_contentid”  
has\_many :countries, :through =\> :web\_content\_associations  
end

class WebContentAssociation  
belongs\_to :web\_content, :foreign\_key =\> “web\_contentid”  
belongs\_to :country, :foreign\_key =\> “details”, :conditions =\>  
“type=‘country’”  
end

class Country  
has\_many :web\_content\_associations, :foreign\_key =\>  
“details”, :conditions =\> “type=‘country’”, :dependent =\> :destroy  
has\_many :web\_contents, :through =\> :web\_content\_associations  
end

p web\_content.countries

i cannnot do something like

class WebContent  
has\_many :web\_content\_associations, :foreign\_key =\> “web\_contentid”  
has\_many :associations, :through =\> :web\_content\_associations  
end

class WebContentAssociation  
belongs\_to :web\_content, :foreign\_key =\> “web\_contentid”  
belongs\_to :association, :polymorphic =\> true, :foreign\_type =\>  
“type”, :foreign\_key =\> “details”  
end

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

p web\_content.countries

as far as i can tell this is because

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

pins the sql to

‘where association\_id=xx and association\_type=yy’

instead of the

‘where details=xx and type=yy’

which is what i need.

in otherwords i need to configure

has\_many :foos, :as =\> :bars

to _not_ use

‘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/)

we can deny everything, except that we have the possibility of being  
better. simply reflect on that.  
h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [May 25, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/has-many-polymorphic/138595/2 "2008-05-25T13:31:19Z")

</div>

On 25 May 2008, at 06:39, ara howard wrote:

> 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:** ![ara\_howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ara\_howard](https://www.ruby-forum.com/u/ara_howard)\
**Post date:** [May 25, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/has-many-polymorphic/138595/3 "2008-05-25T16:48:18Z")

</div>

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

> 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.

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

cheers.
