# \[Req\] polymorphic associations howto

**URL:** <https://www.ruby-forum.com/t/req-polymorphic-associations-howto/65333>\
**Category:** Rails\
**Created:** [February 10, 2006, 10:36am UTC](https://www.ruby-forum.com/t/req-polymorphic-associations-howto/65333 "2006-02-10T10:36:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabien\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/71c47a/32.png) [@Fabien\_F](https://www.ruby-forum.com/u/Fabien_F)\
**Post date:** [February 10, 2006, 10:36am UTC](https://www.ruby-forum.com/t/req-polymorphic-associations-howto/65333/1 "2006-02-10T10:36:45Z")

</div>

Hi people,

I’m looking into polymorphic associations as a way to cleanup some  
messy parts of my code. I think they will allow me to do exectly what  
I need, but since there’s little to none written on this subject, I’d be  
delighted if someone could shed some light on this. Especially what  
the different options and needed table structures need to be.

Thanks,

- Fabien

---

<div class="post-metadata">

**Author:** ![Fabien\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/71c47a/32.png) [@Fabien\_F](https://www.ruby-forum.com/u/Fabien_F)\
**Post date:** [February 10, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/req-polymorphic-associations-howto/65333/2 "2006-02-10T15:09:28Z")

</div>

On 10 Feb 2006, at 10:35, Fabien F. wrote:

> Hi people,
> 
> I’m looking into polymorphic associations as a way to cleanup some  
> messy parts of my code. I think they will allow me to do exectly what  
> I need, but since there’s little to none written on this subject,  
> I’d be  
> delighted if someone could shed some light on this. Especially what  
> the different options and needed table structures need to be.

Chad F. has a rails recipe in the works talking about polymorphic  
associations. It should be in one of the next beta versions of the  
book. I can highly recommend it, it goes there where the Agile book  
stops.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Fabien\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/71c47a/32.png) [@Fabien\_F](https://www.ruby-forum.com/u/Fabien_F)\
**Post date:** [February 10, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/req-polymorphic-associations-howto/65333/3 "2006-02-10T16:59:28Z")

</div>

On 2/10/06, Peter De Berdt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > the different options and needed table structures need to be.
> 
> Chad F. has a rails recipe in the works talking about polymorphic  
> associations. It should be in one of the next beta versions of the  
> book. I can highly recommend it, it goes there where the Agile book  
> stops.
> 
> Best regards
> 
> Peter De Berdt

Polymorphic joins are kind of like an inverted STI setup. Before, I’d  
have to use STI to join a single Asset model to multiple models  
(Articles, Books, Albums, for example).

class Asset \< ActiveRecord::Base  
end

class ArticleAsset \< ActiveRecord::Base  
belongs\_to :article, :foreign\_key =\> ‘object\_id’  
end

class Article \< ActiveRecord::Base  
has\_many :assets, :class\_name =\> ‘ArticleAsset’  
end

Using polymorphic joins gets rid of the redundant \*Asset models:

class Asset \< ActiveRecord::Base

# describes a generic ‘attachable’ class

# uses attachable\_id as foreign key, and attachable\_type

belongs\_to :attachable, :polymorphic =\> true  
end

class Article \< ActiveRecord::Base  
has\_many :assets, :as =\> :attachable  
end

Don’t let this stop you from getting the Rails Recipe book. I can’t  
recommend it enough. And, I’m quite sure Chad’s explanation will be  
much better 😉

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![Fabien\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/71c47a/32.png) [@Fabien\_F](https://www.ruby-forum.com/u/Fabien_F)\
**Post date:** [February 11, 2006, 7:17am UTC](https://www.ruby-forum.com/t/req-polymorphic-associations-howto/65333/4 "2006-02-11T07:17:53Z")

</div>

> class Article \< ActiveRecord::Base  
> has\_many :assets, :class\_name =\> ‘ArticleAsset’  
> end

Thanks Rick,

This is exactly the kind of info I’m looking for. I also used STI to  
solve  
this kind of thing before; it’s the ‘mess’ I was talking about 😉

Looking forward to see it revealed further in the beta book!

- Fabien
