# One-to-one relationship confusion

**URL:** <https://www.ruby-forum.com/t/one-to-one-relationship-confusion/57227>\
**Category:** Rails\
**Created:** [April 8, 2006, 4:38am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-confusion/57227 "2006-04-08T04:38:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 8, 2006, 4:38am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-confusion/57227/1 "2006-04-08T04:38:43Z")

</div>

Hello,

I posted a similar email, earlier in the week, but I still must not be  
getting it.

I have a customer that aggregates two addresses; a shipping address  
and a billing address. Addresses can also be referenced by other  
objects in a similar way, so I would really like to avoid putting the  
keys referencing the customer or other objects in the addresses table.  
Addresses are exclusively referenced by parent objects and a  
many-to-many association is not appropriate.

Ideally I would like the customers table to use a shipping\_address\_id  
and a billing\_address\_id to reference the aggregate addresses. For  
illustration purposes, another object, company might have a  
main\_address\_id, an after\_hours\_address\_id and a weekend\_address\_id.

In order to keep the keys in the customer table the belongs\_to  
statements are in the customer model and the has\_one statement is in  
the address model.

This creates the structure I am after, but not the behavior. The  
customer is really the parent object, when I delete the customer I  
want it to cascade to addresses, etc.

It seems like I am writing more code than I should to propagate  
updates, etc to addresses. Am I totally off base? Any guidance would  
be greatly appreciated.

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 8, 2006, 6:24am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-confusion/57227/2 "2006-04-08T06:24:31Z")

</div>

One solution might be to create a many-to-many relationship for each  
type of object that can have an address, but only ever put one address  
in the collection. So there would be an address\_customer table with  
columns address\_id, customer\_id, and maybe address\_type (indicating if  
it is billing or shipping). Then you would have tables such as  
address\_supplier, address\_employee, etc.

It’s a little more work up front, but the nice thing is that this would  
be very flexible for future changes.

Mike J.

---

<div class="post-metadata">

**Author:** ![dkam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@dkam](https://www.ruby-forum.com/u/dkam)\
**Post date:** [April 8, 2006, 8:59am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-confusion/57227/3 "2006-04-08T08:59:08Z")

</div>

Dan M. wrote:

> This creates the structure I am after, but not the behavior. The  
> customer is really the parent object, when I delete the customer I  
> want it to cascade to addresses, etc.  
> You could use a before\_destroy callback in the customer for this.

class Customer \< ActiveRecord::Base  
belongs\_to :shipping\_address…  
belongs\_to :billing\_address…  
before\_destroy :clear\_addresses  
private  
def clear\_addresses  
self.shipping\_address.destroy  
self.billing\_address.destroy  
end  
end
