# DB Best Practices: Contacts

**URL:** <https://www.ruby-forum.com/t/db-best-practices-contacts/81400>\
**Category:** Rails\
**Created:** [January 8, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/db-best-practices-contacts/81400 "2007-01-08T20:38:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [January 8, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/db-best-practices-contacts/81400/1 "2007-01-08T20:38:41Z")

</div>

While not 100% Rails related I had a quick question while designing my  
app…

Whats the best practice for contacts and addresses. I would assume a  
company has many addresses and contacts? How do some of you deal with  
this?

In my case, I have many companies, each with different addresses (ship  
to and bill to) as well as different contacts at these addresses.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [January 8, 2007, 8:58pm UTC](https://www.ruby-forum.com/t/db-best-practices-contacts/81400/2 "2007-01-08T20:58:43Z")

</div>

Hi !

2007/1/8, Shai S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> While not 100% Rails related I had a quick question while designing my  
> app…
> 
> Whats the best practice for contacts and addresses. I would assume a  
> company has many addresses and contacts? How do some of you deal with  
> this?
> 
> In my case, I have many companies, each with different addresses (ship  
> to and bill to) as well as different contacts at these addresses.

Address should be a full-fledged model, with a polymorphic association  
to Company and/or Contact and/or Invoice. If you want to reuse  
addresses, you’ll need a join model between contacts et. al and  
Address.

As you said, this is really data modelling, and not Rails. But great  
discussion should ensue anyway !

## Bye !

François Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [January 9, 2007, 2:58am UTC](https://www.ruby-forum.com/t/db-best-practices-contacts/81400/3 "2007-01-09T02:58:12Z")

</div>

Maybe you shouldn’t be looking for best practice. It’s going to cost  
you big.  
You could, for example, stick to the sample data you have and put the 2  
addresses directly in the company table. And then relate the contacts  
to this table.  
The problem here is if you get another branch of the same company.  
You’ll have to rename both branch in “Megacorp Shanghai” and “Megacorp  
NY” both entries being unrelated in your data model, and both  
containing a billing address that may be the same…  
It’s up to you, but take everything in consideration and go for the  
simplest your-industry-specific time-and-money-saving-model you can.

Think about the way you are going to import and enter your existing  
data, too. It can be a real nightmare if you play it too “100%”.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [January 9, 2007, 12:32pm UTC](https://www.ruby-forum.com/t/db-best-practices-contacts/81400/4 "2007-01-09T12:32:15Z")

</div>

Hi,

don’t have experience regarding this with Rails, but based on  
experience with Siebel (a CRM package) you could go with something  
like this (I don’t have much experience with Rails yet, so bear with  
me):

You would have your three tables:  
companies { |t|  
t.column :bill\_to\_id  
t.column :ship\_to\_id

# more columns

}  
contacts { |t|  
t.column :company\_id  
t.column :business\_addr\_id

# more columns

}  
addresses { |t|  
t.column :company\_id

# more columns

}

Each address would belong to a company and you wouldn’t want to delete  
addresses (maybe just a deleted flag or a status field). So a company  
could have maybe just one, maybe two, maybe 16 addresses (old  
addresses, different office locations, …)  
Only one of those addresses would be your bill to address and only one  
would be the ship to address (in Siebel these are called something  
like “Primary Ship To Address” and “Primary Bill To Address”). To  
select them you could pick one of the company’s addresses from a drop  
down list. You could select the same address for both resulting in  
company.bill\_to\_id == company.ship\_to\_id or it could be different  
addresses.  
There would be joins company.bill\_to\_id -\> address.id and  
company.ship\_to\_id -\> address.id and a 1:many relationship from  
company.id -\> address.company\_id

Now, each contact would be associated with a company. When you select  
the contacts address, you again pick one of the company’s addresses  
from a drop down list. (You might even have the HQ address in  
company.addr\_id and use that as a default for the contact)  
There would be a join from contact.business\_addr\_id -\> address.id

Orders could be handled similarly. They would belong to a company and  
the default bill to and ship to addresses would be the primary ones of  
the company (company.bill\_to\_id and company.ship\_to\_id). But you could  
change those and pick another address from a drop down list of company  
addresses and populate order.bill\_to\_id and order.ship\_to\_id  
accordingly.

Let me know if you’ve got questions about this.

Thomas
