Forum: Ruby on Rails tying table

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
David M. (Guest)
on 2007-05-16 19:32
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:


Org_Addresses (tying table)



Ind_Addresses (tying table)


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.
Stephen B. (Guest)
on 2007-05-16 19:47
(Received via mailing list)

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:

Then you need to create the join table:


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

class Addresses < ActiveRecord::Base
  has_and_belongs_to_many :organizations

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.

David M. (Guest)
on 2007-05-16 20:33
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
Stephen B. (Guest)
on 2007-05-16 21:17
(Received via mailing list)
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])

   # Find the existing organization
   organization = Organization.find_by_id(params[:organization_id])

   # Associate the address with this organization
   organization.addresses << address

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


David M. (Guest)
on 2007-05-17 17:41

It's all coming together real nicely.  I'm probably over using the join
table concept now.  :)
This topic is locked and can not be replied to.