Forum: Ruby on Rails tying table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0612733748b42ad784fdfd27ee87427f?d=identicon&s=25 David Modernfossil (modernfossil)
on 2007-05-16 17: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:


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.
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-05-16 17:47
(Received via mailing list)
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
0612733748b42ad784fdfd27ee87427f?d=identicon&s=25 David Modernfossil (modernfossil)
on 2007-05-16 18: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
association?
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2007-05-16 19: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.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
www.curve21.com
0612733748b42ad784fdfd27ee87427f?d=identicon&s=25 David Modernfossil (modernfossil)
on 2007-05-17 15:41
Thanks...

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.