Topic: Three-way model relationship

Hello,

I have a small problem with model relations that I'd like some help to work out. Bacically I have a "Projects" model which is made up of "organizations" with specific "roles". Every project can have many organizations contributing, each with a specific role. Also, an organization can have different roles for different projects. I have three tables (projects, organizations, and roles) and I'm thinking about adding a fourth called, say, Assignments, that would contain the foreign keys for all three tables.

So I'm kinda stuck in a decision to go with a habtm or a :through relationship between the three entities.

Any thoughts?

Re: Three-way model relationship

well, you almost have to use a has many :through association to have different orgs, projects, and roles.  I use :through associations almost exclusively now, having an actual model there is nice, especially when you add more attributes later.

I'd use a :through

-Drew

Re: Three-way model relationship

I agree with Drew, definitely go with has_many :through. HABTM won't even work here because that just deals with two tables - not three. Adding the Assignment model is the right way to go. I was going to suggest that before I got to reading that part. smile

Railscasts - Free Ruby on Rails Screencasts

Re: Three-way model relationship

Thanks guys for the help.

One quick question. Assignment model--would that be just a model class or a model class and a database table?

***EDIT..never mind, of course it would have to be a table, how else would the data persist?...silly me

Last edited by tconley (2007-04-13 09:48:05)

Re: Three-way model relationship

Yep, a table, with atleast 3 rows:

project_id
organization_id
role

You can also cram other useful stuff in there if you want.  I've got a join model with 6 other columns in it.

-Drew