Rails Nested Attributes, Relationship for Shared or Common Object

Posted by SooDesuNe on Stack Overflow See other posts from Stack Overflow or by SooDesuNe
Published on 2010-06-14T05:57:51Z Indexed on 2010/06/14 6:02 UTC
Read the original article Hit count: 243

This has to be a common problem, so I'm surprised that Google didn't turn up more answers. I'm working on a rails app that has several different kinds of entities, those entities by need a relation to a different entity. For example:

  1. Address: a Model that stores the details of a street address (this is my shared entity)
  2. PersonContact: a Model that includes things like home phone, cell phone and email address. This model needs to have an address associated with it
  3. DogContact: Obviously, if you want to contact a dog, you have to go to where it lives.

So, PersonContact and DogContact should have foreign keys to Address. Even, though they are really the "owning" object of Address. This would be fine, except that accepts_nested_attributes_for is counting on the foreign key being in Address to work correctly.

What's the correct strategy to keep the foreign key in Address, but have PersonContact and DogContact be the owning objects?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about foreign-key-relationship