# Polymorphic Assosiations

**URL:** <https://www.ruby-forum.com/t/polymorphic-assosiations/193421>\
**Category:** Rails\
**Created:** [August 17, 2010, 2:37pm UTC](https://www.ruby-forum.com/t/polymorphic-assosiations/193421 "2010-08-17T14:37:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![the-lar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@the-lar](https://www.ruby-forum.com/u/the-lar)\
**Post date:** [August 17, 2010, 2:37pm UTC](https://www.ruby-forum.com/t/polymorphic-assosiations/193421/1 "2010-08-17T14:37:22Z")

</div>

I have doubts on Polymorphic Assosiations.  
On Which situations we are using these??  
What are the advantages?

---

<div class="post-metadata">

**Author:** ![the-lar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@the-lar](https://www.ruby-forum.com/u/the-lar)\
**Post date:** [August 17, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/polymorphic-assosiations/193421/2 "2010-08-17T14:52:33Z")

</div>

On 17 Aug 2010, at 13:37, Sateesh K. wrote:

> I have doubts on Polymorphic Assosiations.  
> On Which situations we are using these??

I used these recently for a pinboard. We have an internal system that  
contains contact management, support tickets, products & services,  
billing etc. These are all different represented by different active  
record models.

Users wanted to be able to pin arbitrary objects to an area of the  
site/app that would be always available. For example the could have a  
couple of support tickets, a contact record and maybe a few service  
records all accessible in the same left hand div anywhere in the app.

I did this with the following model:

class PinnedItem \< ActiveRecord::Base belongs\_to :user belongs\_to :pinned, :polymorphic =\> true

validates :pinned\_id, :uniqueness =\> {:scope =\> [:pinned\_type,  
:user\_id]}

if User.current  
default\_scope where(:user\_id =\> User.current.id)  
end  
end

> What are the advantages?

I couldn’t say. This just seemed like the easiest way to accomplish  
this, that is having a normal AR association but with arbitrary rather  
than a specific class of objects.

This is the first time I’ve used polymorphic associations so if there  
any major disadvantages that anyone’s aware of I’d like to know.

Regards,  
Jim

---

<div class="post-metadata">

**Author:** ![the-lar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@the-lar](https://www.ruby-forum.com/u/the-lar)\
**Post date:** [August 17, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/polymorphic-assosiations/193421/3 "2010-08-17T16:26:03Z")

</div>

Sateesh K. wrote:

> I have doubts on Polymorphic Assosiations.  
> On Which situations we are using these??  
> What are the advantages?

I use polymorphic associations all over the place in one application…  
mostly for the reporting and research aspect of the application.

A ‘functional\_area’ model can be related to a project, a testing  
scenario, a specific unit test, a functional requirement, or a marketing  
requirement.

With polymorphic links, I have 1 arealink table that can link a  
functional area to any of those other models.

The same logic applies to ‘modules’, ‘applications’, ‘features’,  
‘functional\_requirements’, etc. The initial ERD I received was almost  
unreadable and looked like a badly done Spirograph drawing (does anyone  
else know what those are anymore?)

When I switched to polymorphic associations, I eliminated 18 separate  
join tables from the design in favor of several polymorphic join tables  
and was able to create 1 ‘linking’ partial (about 30 lines of haml) to  
manage creating/removing links no matter which polymorphic association  
was being altered from a source model to any type of target model. When  
I have the time, I’m going to look into a double-polymorphic join table  
and get those join tables down to 1.

The only downside I’ve seen thus far is that I have to restart my server  
when I add polymorphic join definitions, but that’s no big deal.
