N-way has_mant :through

I’m trying to setup some mildly complex associations for a project we’re
working on and can’t seem to find much documentation on n-way has_many
:through associations.

I have the following models: Person, PhysicalAddress, EmailAddress,
PhoneNumber.
Each person can have multiple PhysicalAddresses, EmailAddresses, and
PhoneNumbers, and multiple people can share the same PhysicalAddress,
EmailAddress, or PhoneNumber.

I need to track the types of associations (i.e. home, work, cell, etc)
for each, so habtm definitely won’t cut it.

Do I need to setup separate join tables for each association
(people_physical_addresses, people_phone_numbers, etc), or should I
have a ‘contacts’ (for lack of a better name at the moment) table with
several columns for person_id, physical_address_id, phone_number_id,
email_address_id, & contact_type. Or should I do something else
entirely?

Thanks!

Jonathan M. wrote:

I’m trying to setup some mildly complex associations for a project we’re
working on and can’t seem to find much documentation on n-way has_many
:through associations.

I have the following models: Person, PhysicalAddress, EmailAddress,
PhoneNumber.
Each person can have multiple PhysicalAddresses, EmailAddresses, and
PhoneNumbers, and multiple people can share the same PhysicalAddress,
EmailAddress, or PhoneNumber.

I need to track the types of associations (i.e. home, work, cell, etc)
for each, so habtm definitely won’t cut it.

Do I need to setup separate join tables for each association
(people_physical_addresses, people_phone_numbers, etc), or should I
have a ‘contacts’ (for lack of a better name at the moment) table with
several columns for person_id, physical_address_id, phone_number_id,
email_address_id, & contact_type. Or should I do something else
entirely?

You can do an n-way join using a single join table. One of your models
might look something like this:

Person
has_many :contacts
has_many :addresses, :through => :contacts
has_many :emails, :through => :contacts
has_many :phones, :through => :contacts

You have to be careful how you model things, and think hard about the
nature of the Contact object. Do you really want a person’s email
address to be tied to his phone number and physical address, or are they
independent of each other? Would a Person ever have more than one
Contact, with another set of address/email/phone data?


Josh S.
http://blog.hasmanythrough.com

Josh S. wrote:

nature of the Contact object. Do you really want a person’s email
address to be tied to his phone number and physical address, or are they
independent of each other? Would a Person ever have more than one
Contact, with another set of address/email/phone data?

Thanks Josh. It wouldn’t hurt to be able to tie emails to phones to
addresses, but we probably won’t use that. We’re mainly interested in
tyeing each person to their many means of contacting them. And since
we’re dealing mostly with families, there’ll be a lot of sharing of
phone numbers and addresses between people. “Contact” probably isn’t the
best term for the join table/object. But whatever name we decide to give
it, yes, each person will only ever have one of them. Maybe “Rolodex”
would be better.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs