Re: Modeling complex associations

MaD
Mon, 19 Jan 2009 12:17:12 -0000

be careful: you''re building a ring. that''s a complex and so
trivial
thing.

obvious problem: for a company to store the foreign key of its
headquarter the building has to exist. for that building to store the
foreign key of the company that it belongs to, the company has to
exist.

to simplify it, there are a couple of solutions:
- you could just use an additional attribute in building
"is_headquarter" (bool) <-- your mentioned headquarter flag
- you could add an additional model headquarter that has many
branches, such that "a company has_one headquarter" and "it
has_many
branches through headquarter".  <-- problem here: hard to upgrade a
branch to headquarter and data-redundancy
- you could create a polymorphic building with an interface for
headquarter and branch
http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations

imho the last option seems to be the best.