This question on SO appears to be related to my question, but I'm not sure my question is answered by that.
An address can belong to more than one model (UserProfile and Event)
What's the correct way to implement this?
The basic tables:
user_profiles(id)
events(id)
Options for implementing the addresses table:
addresses(id,user_profile_id,event_id)
This approach seems to be kludgy since if tomorrow the address needs to belong to one more model, I have to add that id field.
Also, I don't know yet, but adding a new id field may cause some code to break as well ?addresses(id,model_type,model_id)
This is polymorphic, right. I'm don't know why, but I feel wary of this somehow?Some other way to do this?
Note:
I could have made the tables like this, I suppose:
user_profiles(id,address_id)
events(id,address_id)
But, this means that the same address_id
can belong to different models.
I suppose it should not be that way, because say for example that the address for the event needs to be changed, but it should not affect the address of the user_profile
.
So that would be something like this (which I think is wrong):
@current_user_profile.address = some_new_address
#this would have changed the address for both the user_profile *and* the event
@current_user_profile.save