# Application and Database Design

**URL:** <https://www.ruby-forum.com/t/application-and-database-design/57831>\
**Category:** Rails\
**Created:** [April 14, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/application-and-database-design/57831 "2006-04-14T17:20:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [April 14, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/application-and-database-design/57831/1 "2006-04-14T17:20:58Z")

</div>

Hi,

I am begining the design of an order management system, and had a few  
questions  
about the database design. For example: I have a customers table and an  
orders  
table, with a one to many relationship. Now if i change the customers  
address  
after the order has been created, it will be updated in any of the past  
orders,  
and I do not wish this to happen. Is the best way to deal with this to  
store the  
customers information in the order table, or is there an easy way to set  
up some  
sort of versioning system for the customer within rails?

Thanks,  
Ryan

---

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [April 14, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/application-and-database-design/57831/2 "2006-04-14T17:39:13Z")

</div>

I think that the easiest way to do this is create an addresses table  
with relationships to both the customers and the orders. It would have  
many addresses for both customers and orders (one to many relations).  
This is how you set up a versioning system. It also requires that you  
put into the addresses table some indicator of a preferred address for  
the customer so that it is easy to know which is the default.

Others may have some other ideas.

Ben Orchard

---

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [April 14, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/application-and-database-design/57831/3 "2006-04-14T17:42:19Z")

</div>

On 4/14/06, Ryan L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,  
> Ryan
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Hi Ryan

> From your description, I think the simplest, best solution would be to  
> “stamp” the current customer address into any order created. That way  
> the address “stays” with the order, independtly of the client. You can  
> however retrieve all past orders of the client, and hence all his past  
> addresses, so you in effect get all the benefits of versioning without  
> the costs.

Regards,  
Alder

---

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [April 14, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/application-and-database-design/57831/4 "2006-04-14T18:24:12Z")

</div>

Ryan L. wrote:

> I am begining the design of an order management system, and had a few  
> questions  
> about the database design. For example: I have a customers table and an  
> orders  
> table, with a one to many relationship. Now if i change the customers  
> address  
> after the order has been created, it will be updated in any of the past  
> orders,  
> and I do not wish this to happen. Is the best way to deal with this to  
> store the  
> customers information in the order table, or is there an easy way to set  
> up some  
> sort of versioning system for the customer within rails?

Check out the acts\_as\_versioned plugin

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![lazukars](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eb9ed0/32.png) [@lazukars](https://www.ruby-forum.com/u/lazukars)\
**Post date:** [April 14, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/application-and-database-design/57831/5 "2006-04-14T18:11:41Z")

</div>

An address table is one good idea, but keep in mind that if you want to  
meet your first goal of keeping the addresses used on invoices, you’d  
have to save any “edited” address as a new address entry and retain  
fields for marking which ones are “current”, maybe one for “which” of  
the users’ saved addresses this is (home, work, gramdma,etc.), and  
another for the version, so that an order going to Mary’s home address  
in April stays linked to the April home address.

A much simpler but less flexible solution is to have address fields in  
the order itself and copy them from the User table at the time the order  
is saved. This way addresses and even users can be modified and deleted  
to your heart’s content, but the address info stays with the record of  
the order.

The disadvantage to the simpler approach is that it doesn’t offer a  
clean way to generate a report on all the orders shipped to a given  
address, for instance. But then, plenty of e-commerce sites get by just  
fine without such things. It’s all a matter of how much complexity you  
reasonably expect to use.

Ryan L. wrote:

> Hi,
> 
> I am begining the design of an order management system, and had a few  
> questions  
> about the database design. For example: I have a customers table and an  
> orders  
> table, with a one to many relationship. Now if i change the customers  
> address  
> after the order has been created, it will be updated in any of the past  
> orders,  
> and I do not wish this to happen. Is the best way to deal with this to  
> store the  
> customers information in the order table, or is there an easy way to set  
> up some  
> sort of versioning system for the customer within rails?
> 
> Thanks,  
> Ryan
