# Has Many - Polymorphism Help

**URL:** <https://www.ruby-forum.com/t/has-many-polymorphism-help/111640>\
**Category:** Rails\
**Created:** [August 9, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/has-many-polymorphism-help/111640 "2007-08-09T13:09:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 9, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/has-many-polymorphism-help/111640/1 "2007-08-09T13:09:45Z")

</div>

Hello,

I am working with a classification system. The client’s needs are  
outpacing my understanding of Rails model relationship concepts.

There are sectors, and subsectors:  
A sector has\_many subsectors and a subsector belongs\_to a sector.

I also have users and organizations:  
An organization has\_and\_belongs\_to\_many :users, :through  
=\> :positions and a user  
has\_and\_belongs\_to\_many :organizations, :through  
=\> :positions. :positions is a model which defines the nature of the  
user/organization relationship (for example job\_title).

So far I have it all working as it should using the techniques  
described.

Here’s the catch…

Both an organization and a user need to be able to have up to four  
subsector classifcations. These four can include sectors. So I am  
trying to relate 4 different tables into some sort of model. If it  
were just User \<-\> Subsector and Organization \<-\> Subsector, I would  
make a model for each relationship, but the choices need to include  
the sector themselves.

It may seem like tagging is an option, but this classification system  
is in addition to a tagging system that the users and organizations  
can use to further define themselves.

I have been reading about polymorphism and the has\_many\_polymorphs  
plugin, but I do not fully understand how to implement the concept  
into my situation.

Any suggestions?

Thanks!

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 9, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/has-many-polymorphism-help/111640/2 "2007-08-09T13:25:45Z")

</div>

Also, (while I’m at it) the entire point of this process is to be able  
to browse through Organizations by Sectors and then Subsectors, so how  
would you approach sorting the data out into the views?

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 9, 2007, 1:43pm UTC](https://www.ruby-forum.com/t/has-many-polymorphism-help/111640/3 "2007-08-09T13:43:47Z")

</div>

You might wanna pop in at #fauna ( official has\_many\_polymorphs  
channel ) at [irc.freenode.net](http://irc.freenode.net) and ask for help there.

On 8/9/07, Mindtonic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > So far I have it all working as it should using the techniques
> > 
> > Thanks!
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 9, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/has-many-polymorphism-help/111640/4 "2007-08-09T16:34:13Z")

</div>

Mindtonic, also consider that maybe you could handle this with self-  
referential tables. ie. Organisation habtm Organisation. And then  
use the acts\_as\_tree plugin if you want. That way you can have  
abitrarily nested organisational relationships and still keep your  
model quite simple. Same for the user side (habtm users).

Here’s an example definition in case it helps (and sorry if you know  
this stuff backwards and I’m wasting your bandwidth)

class Organisation  
has\_and\_belongs\_to\_many :related\_organisations,  
:class\_name =\> “Organisation”,  
:join\_table =\> “related\_organisations”,  
:association\_foreign\_key =\> “related\_organisation\_id”,  
:foreign\_key =\> “organisation\_id”

which defines Organisation to habtm of itself through a join table.

Cheers, --Kip

---

<div class="post-metadata">

**Author:** ![mindtonic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f19dbf/32.png) [@mindtonic](https://www.ruby-forum.com/u/mindtonic)\
**Post date:** [August 9, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/has-many-polymorphism-help/111640/5 "2007-08-09T17:17:17Z")

</div>

Thanks for bringing the acts\_as\_tree plugin to my attention. I will  
make a note of it and look at it. I am sure it will come in handy in  
the future as new classification systems are always being devised.

I ended up using the has\_many\_polymorphs plugin as a solution.

In the end it turns out I was just creating a different kind of  
tagging system, one that is backwards from the traditional model.  
Since I am also building a traditional tagging system to accompany my  
sectors, has\_many\_polymorphs will solve both problems.

I sincerely thank the fine folks at fauna for their direct assistance,  
it was wonderful to talk to a human.
