# Has\_many :through with multiple paths

**URL:** <https://www.ruby-forum.com/t/has-many-through-with-multiple-paths/100345>\
**Category:** Rails\
**Created:** [July 28, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/has-many-through-with-multiple-paths/100345 "2006-07-28T14:54:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rurounijones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rurounijones](https://www.ruby-forum.com/u/rurounijones)\
**Post date:** [July 28, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/has-many-through-with-multiple-paths/100345/1 "2006-07-28T14:54:54Z")

</div>

Hi all.

I am currently thinking about how to do the following:

I have the following models.  
Team  
Person

Team has various positions (manager, programmer etc.) I would like each  
one of those positions to reference one or more Person records. A single  
Person record could be on 6 different teams at the same time in  
different roles. the same person could even be on the same team multiple  
times in different roles.

I am not 100% sure how to go about this. I am thinking that I will need  
something like an intermediate record.

teams\_people  
id  
team\_id  
people\_id  
role

I would then have (I think) to have methods in the Team model that will  
find each person in the @team.people array based on the role.

I the end I want a view to have something like:

Team Name  
Manager: Person1, Person 4  
Programmer: Person2, Person 3  
Designer: Person2  
Lackey: Person3

With just a single find that :includes all the relevant people.

I apologise if this isn’t very clear. The joys of not being a Ruby  
programmer but a Ruby dabbler 😉

RJ

---

<div class="post-metadata">

**Author:** ![rurounijones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rurounijones](https://www.ruby-forum.com/u/rurounijones)\
**Post date:** [July 28, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/has-many-through-with-multiple-paths/100345/2 "2006-07-28T17:05:45Z")

</div>

RurouniJones wrote:

> Hi all.
> 
> I am currently thinking about how to do the following:
> 
> I have the following models.  
> Team  
> Person
> 
> Team has various positions (manager, programmer etc.) I would like each  
> one of those positions to reference one or more Person records. A single  
> Person record could be on 6 different teams at the same time in  
> different roles. the same person could even be on the same team multiple  
> times in different roles.
> 
> I am not 100% sure how to go about this. I am thinking that I will need  
> something like an intermediate record.
> 
> teams\_people  
> id  
> team\_id  
> people\_id  
> role
> 
> I would then have (I think) to have methods in the Team model that will  
> find each person in the @team.people array based on the role.
> 
> I the end I want a view to have something like:
> 
> Team Name  
> Manager: Person1, Person 4  
> Programmer: Person2, Person 3  
> Designer: Person2  
> Lackey: Person3
> 
> With just a single find that :includes all the relevant people.

You’re definitely on the right track. I’d suggest naming the join model  
something like Membership (table name ‘memberships’ instead of  
‘teams\_people’), which is the way to go when using has\_many :through  
instead of habtm. Your use of the ‘role’ field is exactly what I’d do.  
Here’s something I wrote that talks about working with this kind of join  
model and shows how you can use that to create convenient specialized  
accessors for the various roles:  
[http://blog.hasmanythrough.com/articles/2006/05/06/through\_gets\_uniq](http://blog.hasmanythrough.com/articles/2006/05/06/through_gets_uniq)

I haven’t played around with using :include for that kind of setup, but  
I think you should just be able to :include the various specialized  
associations and it will do the right thing.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![rurounijones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@rurounijones](https://www.ruby-forum.com/u/rurounijones)\
**Post date:** [July 28, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/has-many-through-with-multiple-paths/100345/3 "2006-07-28T17:34:29Z")

</div>

Josh S. wrote:

> I haven’t played around with using :include for that kind of setup, but  
> I think you should just be able to :include the various specialized  
> associations and it will do the right thing.
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

Ye gods!

Thank you very much for the link. It is a stupendously good tutorial for  
this sort of thing. Thank you very much.

Do you have plans on adding that to the wiki.rubyonrails site? (Or  
create a page that then links to the blog?) I use the Wiki site  
extensively (as I imagine others do to) and a page that has this  
information would be great.

Many thanks

RJ
