Has Many - Polymorphism Help


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

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?


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?

You might wanna pop in at #fauna ( official has_many_polymorphs
channel ) at irc.freenode.net and ask for help there.

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

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.