[email protected] wrote:
User has_many groups
Group belongs_to User
Group has_many addresses
Address belongs_to Group
Is this a (healthy) design? Is it better to create a join table?
The great thing about migrations (and unit tests) is you are not stuck
answers. You can deploy, change your database, and deploy again, without
your users to re-input all their data.
A group has a name, for your user to tag contacts with. So if an
table” (for has_and_belongs_to_many) can’t have any attributes, you need
However, your current system is not dry. Users have many Groups, and
and belong to many Addresses. If you supply one anonymous Group per User
hold the Contacts that a User has not yet aligned with any Groups, then
don’t need Addresses that belong to Users and Groups at the same time.
ownership is bad like Matrix Management; Contacts should only belong to
User 1–>* Group <–> Contact
Group.has_and_belongs_to_many :contacts # and migrate contacts_groups
User.has_many :contacts, :through => :groups
Contact.has_one :user, :through => :groups
Now report back if the last two lines work. I have never gotten a
working with a habtm! If they don’t work, trivially write accessors to
And remember to create one anonymous Group each time you create a User.