How do I model a has_many :through and with aggregation in Rails?

Posted by Angela on Stack Overflow See other posts from Stack Overflow or by Angela
Published on 2010-05-09T03:19:03Z Indexed on 2010/05/09 3:28 UTC
Read the original article Hit count: 236

How do I model having multiple Addresses for a Company and assign a single Address to a Contact?

Contacts belong_to a Company. A Company has_many Contacts.

A Company also has_many Addresses. And each Contact belongs_to an Address.

How do I model this?

I have

Model/Contacts.rb

belong_to :Company
belong_to :Address (?)

Model/Company.rb

has_many :Contacts
has_many :Addresses

Address is an aggregation of :street1, :street2, :city, :state, :zip so not clear exactly what to do there.

So what would I do in my _form so that when I have a contact/new I am able to either default to a main address or select one of the others? If none of them match, adding for a Contact makes that address available to any subsequent contact?

© Stack Overflow or respective owner

Related posts about has-many-through

    Related posts about aggregation