Topic: Multiple HABTM Model Associations

Hi all smile

I am having some trouble with my model associations.  I have 3 main models.  The application is not centered around a single one, but all of them.  It is a sort of love triangle.  This has made it a little hard for me, because I do not have one starting point.  For instance, a Company has many Projects, but each Project has many companies working on it.  I am trying to decide between has_many :through and habtm.  Whenever I see justification for has_many :through, there is always a good joiner table.  I can't find one here though.  Associated?  Any help would be greatly appreciated!  Also, if anyone knows of an example of something like this somewhere, I would love to take a look at it!

Re: Multiple HABTM Model Associations

I think HABTM is phasing out in favor of has_many :through. HABTM is convenient at first, but if you ever need to extend the join table with more attributes then you will have problem. You can always refactor to a has_many :through relationship after the fact, but that can be a pain.

If there's any chance at all that you will need more attributes in the Company to Project join relationship, then I would use has_many :through. As for the name of the join, I don't know. Association seems okay, but it is very generic. There's always CompanyProject, but I would use that as a last resort.

Railscasts - Free Ruby on Rails Screencasts

Re: Multiple HABTM Model Associations

Thanks Ryan smile

Re: Multiple HABTM Model Associations

Would "Contracts" be a good name for the join model between Companies and Projects?  "WorkEfforts", "Taskings", etc.  I'm not sure exactly what nuance you need, but you might be able to find something in words like those....

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Multiple HABTM Model Associations

NielsenE, Contracts may be perfect!  Thanks smile

Re: Multiple HABTM Model Associations

Here's another question that came up.  A company has products, but there are different types of companies.  There are different ways that a company can be associated with the product, whether it be through manufacturing or distribution.  Should I have one relationship between products and companies, or should I split it up in to each type?  I could just link the product to multiple companies and within the company model, I would specify what type of company it is.  Or would I want to do this in the joining table?  The other option would be to link the product to each type of company:  a product has one manufacturer, but it has many distributors and installers.  Thanks smile

Re: Multiple HABTM Model Associations

Do you store different data for Manufacturers, Distributes, and Installers, or is it only a "qualifier" on the relationship of the Product to Company relationship?

If there is no additional information, I'l probably extend the join model to hold a "company_type"  (avoiding STI "type" as you wouldn't need to subtype the Companies I think).  Then set up 2-3 different has_many throughs with conditions to restrict to the subset appropriate.  If you need extra data, or different functional behavior for the different company types go with a full STI approach.

Do all products have one and only one manufacturer?

If yes, I'd add a manufactorer_id to your product table and setup a belongs_to/has_one association for that and remove it from the STI/typee join model.  Otherwise its safe to treat manufacturer as just another type.

My RoR journey  -- thoughts on learning RoR and lessons learned in applying TDD and agile practices.

Re: Multiple HABTM Model Associations

I do not store different data for the manufacturers, distributors, or installers.  It just differentiates the type of company, and in the end, how they are related to the product.  I'm not sure exactly how the manufacturer will work out yet.  But for now I think I'll probably leave it in as a type, because I may wish to keep this functionality in.  Thanks again for all the help smile