Rails Nested Attributes, Relationship for Shared or Common Object -
This should be a common problem, so I'm surprised Google has not left any more responses. I am having a lot of different types of institutions, those institutions need the relationship of a separate entity. For example:
-
Address
: A model that stores the street address details (this is my shared entity) -
PersonContact
: A model that includes things like home phone, cell phone and email address -
DogContact
: Obviously, if you want to contact a dog , Then you will have to go to the place where it lives.
Therefore, PersonContact
and DogContact
must have the foreign key address
. Even though they are actually "master" object of address
, it will be fine, except that accepts __nested_attributes_for
foreign key to Calculating to work correctly in the address
.
The key to keeping foreigners is key in address
, but do you own the PersonContact
and DogContact
?
I believe you should use it.
You should see a addressable_id
and a addressable_type
on your address
and your model should look like this:
class address & lt; ActiveRecord :: Base is_to: addressable ,: polymorphic = & gt; True ending class person contacts & lt; ActiveRecord :: Base has_one: address, as in => : Addressable End Class DogContact & lt; ActiveRecord :: Base has_one: address, as in => : Addressable end
Comments
Post a Comment