# Polymorphic Through Relationships

**URL:** <https://www.ruby-forum.com/t/polymorphic-through-relationships/66631>\
**Category:** Rails\
**Created:** [August 28, 2006, 3:17am UTC](https://www.ruby-forum.com/t/polymorphic-through-relationships/66631 "2006-08-28T03:17:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![s450r1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@s450r1](https://www.ruby-forum.com/u/s450r1)\
**Post date:** [August 28, 2006, 3:17am UTC](https://www.ruby-forum.com/t/polymorphic-through-relationships/66631/1 "2006-08-28T03:17:43Z")

</div>

I’m building a site for a school which has a number of activities and  
groups. Each activity can have a multiple leaders and multiple  
participants. The groups and activities vary widely - some are events,  
some  
are classrooms, some are study groups and some are short-term projects.

Instead of having multiple habtm tables, I thought it would be cleaner  
to  
have a single table called ‘participations’ that would look like:

- participateable\_id
- participateable\_type
- person\_id
- role\_id

The model structure might look like this -

- class Person;
- class Event; has\_many :instructors; has\_many :students;
- class StudyGroup; has\_one :instructor; has\_many :students;

I’d love to be able to say @event.students \<\< Person.find(params[:id])  
and  
have a new row created in participations with the correct person and a  
role  
id of 2 (let’s say). Then when I write @event.instructors \<\<  
Person.find(params[:id])  
it would create a new row with the person id and a role id of 1. Is  
this  
possible using standard rails relationships?

To sum it up, I’m looking for a has\_many :through relationship where the  
“through” is a rails-polymorphic table with one field defined by the  
relationship (role\_id - always the same).

I know how to accomplish this by writing my own methods (which is how I  
have  
it now), but I’d like to know if there is a slick way to do this via  
built-in relationships.

Thanks in advance -

---

<div class="post-metadata">

**Author:** ![s450r1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@s450r1](https://www.ruby-forum.com/u/s450r1)\
**Post date:** [August 28, 2006, 4:37am UTC](https://www.ruby-forum.com/t/polymorphic-through-relationships/66631/2 "2006-08-28T04:37:48Z")

</div>

Hi!

I think you will find the answer at Josh S.'s great blog about it  
[http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation](http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation)

> To sum it up, I’m looking for a has\_many :through relationship where the  
> “through” is a rails-polymorphic table with one field defined by the  
> relationship (role\_id - always the same).

Priit

---

<div class="post-metadata">

**Author:** ![s450r1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@s450r1](https://www.ruby-forum.com/u/s450r1)\
**Post date:** [August 28, 2006, 9:46am UTC](https://www.ruby-forum.com/t/polymorphic-through-relationships/66631/3 "2006-08-28T09:46:11Z")

</div>

That looks promising - and only a few days old. Not bad.
