# Tying table

**URL:** <https://www.ruby-forum.com/t/tying-table/104308>\
**Category:** Rails\
**Created:** [May 16, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/tying-table/104308 "2007-05-16T17:32:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [May 16, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/tying-table/104308/1 "2007-05-16T17:32:05Z")

</div>

I’m not sure if rails can manage this for you, but in the past when I  
have two objects that access the same object I’ve created a tying table  
(not sure if this is the right way to say it).

For example, an organization and an individual both can have many  
addresses. While I could create an org\_addresses and an ind\_addresses  
table I’d rather not manage two tables of addresses when I could just  
create two tying tables.

My tables would look like this:

Organizations  
id

Org\_Addresses (tying table)  
organiation\_id  
addresses\_id

## Address id

Individuals  
id

Ind\_Addresses (tying table)  
individual\_id  
addresses\_id

Address  
id

This would allow me to manage addresses in one place but still be able  
to access the individuals and the organizations addresses by using the  
tying table.

How would one go about doing this in rails? I’ve done this many times,  
but am new to rails.

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [May 16, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/tying-table/104308/2 "2007-05-16T18:33:50Z")

</div>

Great! thanks steve,

one question though…if I then want to force an address to be  
associated with an organization do I need to manage this or does rails  
do that for me? If so, how?

For example, if I want to add a address to an existing organziation does  
rails manage that for me or do I need to specifically code that  
association?

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [May 16, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/tying-table/104308/3 "2007-05-16T17:47:48Z")

</div>

Hey,

The table you call a ‘tying’ table is also called a ‘join’ table. It  
is part of what it is known as a ‘has and belongs to many’  
relationship. The good news is that Rails handles this very easily.

Let’s take the Organizations - Addresses relationship. First you’ll  
create your models and tables. You’ll end up with these tables:  
organizations  
addresses

Then you need to create the join table:

## addresses\_organizations

## address\_id organization\_id

Rails assumes that your join table is named after the 2 related  
classes, in plural and alphabetical order.

Next you tie this up in you models:

class Organizations \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :addresses  
end

class Addresses \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :organizations  
end

That’s it for the tieing up. You can then do things like this:

organization = Organization.find\_by\_name(‘Company name’)

organization.addresses \<\< Address.find\_by\_postcode(‘TE5 TID’)

And Rails will do the rest. Just repeat that for your Individual  
class and you should be good to go.

Steve

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [May 17, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/tying-table/104308/4 "2007-05-17T15:41:54Z")

</div>

Thanks…

It’s all coming together real nicely. I’m probably over using the join  
table concept now. 🙂

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [May 16, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/tying-table/104308/5 "2007-05-16T19:17:57Z")

</div>

Well you kinda need to write the code to add the address. It’d look  
something like this:

Let’s say you’ve got an ‘add address’ form and it posts to an action  
that you want to add the address to an existing organization:

# Create and validate the new address

address = Address.new(params[:address])

if address.save

# Find the existing organization

organization = Organization.find\_by\_id(params[:organization\_id])

# Associate the address with this organization

organization.addresses \<\< address  
end

Obviously there’s a lot more to getting it to work than that code, but  
I hope it demonstrates the concept.

Steve

–  
[www.stephenbartholomew.co.uk](http://www.stephenbartholomew.co.uk)  
[www.curve21.com](http://www.curve21.com)
