# Many to many polymorphic relationships

**URL:** <https://www.ruby-forum.com/t/many-to-many-polymorphic-relationships/172146>\
**Category:** Rails\
**Created:** [July 15, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/many-to-many-polymorphic-relationships/172146 "2009-07-15T17:41:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![talex83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/54ee81/32.png) [@talex83](https://www.ruby-forum.com/u/talex83)\
**Post date:** [July 15, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/many-to-many-polymorphic-relationships/172146/1 "2009-07-15T17:41:32Z")

</div>

I’ve been struggling to get a clear and straight-forward answer on  
this one, I’m hoping this time I’ll get it! 😃 I definitely have a lot  
to learn still with Rails, however I do understand the problem I’m  
facing and would really appreciate additional help.

o I have a model called “Task”.  
o I have an abstract model called “Target”.  
o I would like to relate multiple instances of subclasses of Target to  
Task.  
o I am not using single table inheritance.  
o I would like to query the polymorphic relationship to return a mixed  
result set of subclasses of Target.  
o I would like to query individual instances of subclasses of Target  
to obtain tasks that they are in a relationship with.

So, I figure a polymorphic many to many relationship between Tasks and  
subclasses of Targets is in order. In more detail, I will be able to  
do things like this in the console (and of course elsewhere):

-=-  
task = Task.find(1)  
task.targets  
[…array of all the subclasses of Target here…]  
-=-

But! Assuming models “Store”, “Software”, “Office”, “Vehicle”, which  
are all subclasses of “Target” exist, it would be nice to also  
traverse the relationship in the other direction:

-=-  
store = Store.find(1)  
store.tasks  
[…array of all the Tasks this Store is related to…]  
software = Software.find(18)  
software.tasks  
[…array of all the Tasks this Software is related to…]  
-=-

The database tables implied by polymorphic relationships appears to be  
capable of doing this traversal. The class name is present in the  
relationship table, but I see some recurring themes in trying to find  
an answer which to me defeat the spirit of polymorphic relationships:

o Using my example still, people appear to want to define Store,  
Software, Office, Vehicle in Task, which we can tell right away isn’t  
a polymorphic relationship as it only returns one type of model.  
o Similar to the last point, people still want to define Store,  
Software, Office and Vehicle in Task in one way shape or form. The  
important bit here is that the relationship is blind to the  
subclassing. My polymorphs will initially only be interacted with as  
Targets, not as their individual subclass types. Defining each  
subclass in Task again starts to eat away at the purpose of the  
polymorphic relationship.  
o I see that a model for the join table might be in order, that seems  
somewhat correct to me except that it adds some complexity I assumed  
Rails would be willing to do away with. I plea inexperience on this  
one.  
It seems to be a small hole in either rails functionality or the  
collective community knowledge.

I am also maintaining this question here:

> <https://stackoverflow.com/questions/1128308/rails-many-to-many-polymorphic-relationships>

Thanks to everyone who help!
