ActiveRecord - Multiple Address for a single record

I’m trying to figure out the Rails way to model the following problem:

I have 2 tables. One (let’s call it location) contains a single
address. The other (let’s call it company) contains 3 address. I’m
trying to figure out the best way to model this. I’ve created an
address table, a set up the model as follows:

class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic =>true
end

With the table defined as:

create_table :addresses do |t|
  # t.column :name, :string
  t.column :address_line_1,    :string, :null => false
  t.column :address_line_2,    :string
  t.column :city,              :string, :null => false
  t.column :state,             :string, :null => false
  t.column :zipcode,           :string, :null => false
  t.column :country,           :string
  t.column :addressable_id,    :integer
  t.column :addressable_type,  :string

This would appear to give me the basic polymorphic assocation. Here is
my challange. Mapping to location seems simple:

class Location < ActiveRecord::Base
has_one :address, :as => :addressable
end

What I’m having trouble with is the company model. I’ve currently got:

class Company < ActiveRecord::Base
has_one :main_address, :as => :addressable
has_one :billing_address, :as => :addressable
has_one :alternate_billing_address, :as => :addressable
has_many :locations
end

This is clearly not correct, as each address record only has room to
point back to a single company record. This is a common problem, so I’m
sure a standard solution is out there, I just can’t find it.

Does anyone have any advise on how to model this? Add elements in the
company table point to the address table? If I do that, what should the
model look like.

I’m sure this is a newb problem, so feel free to point me to an example
or some documentation.

Thanks,
Steve

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steve,

Have you solved this yet? If not are you asking how you can have a
Company have many addresses and also have an address belong to
many Companies?

Zach

Steve Webb wrote:

  t.column :addressable_id,    :integer

Does anyone have any advise on how to model this? Add elements in the
company table point to the address table? If I do that, what should the
model look like.

I’m sure this is a newb problem, so feel free to point me to an example
or some documentation.

Thanks,
Steve

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE3VPTMyx0fW1d8G0RAib3AJ9M4zJ4/2v89eLzCC1jBKyJsebVWQCdH5Sa
tokaJJ3bIDtFkfbvwVDYXWs=
=/DSB
-----END PGP SIGNATURE-----

just so i understand, you want to be able to have a single address map
to a company and a location, but under your example, that is not
doable as each address can only map to one object, either a location
or a company…

why not have the company’s main address map through location and it’s
other addresses map directly to the address table?

ie

address.rb

class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end

billing_address.rb

class BillingAddress < Address
end

alternate_billing_address.rb

class AlternateBillingAddress < Address
end

location.rb

class Location < ActiveRecord::Base
has_one :address, :as => :addressable
belongs_to :company
end

main_location.rb

class MainLocation < Location
end

#company.rb
class Company < ActiveRecord::Base
has_many :locations
has_one :main_location
has_one :billing_address, :as => :addressable
has_one :alternate_billing_address, :as => :addressable
end

the billing_address, alternate_billing_address and main_location are
going to have to be created by hand as they are just extensions of the
base active record model.

the address table will have to have an ‘addressable_id’,
‘addressable_type’ and ‘type’

the addressable_id and addressable type will contain the id and type
of the associated object (Location, MainLocation or Company), the type
column will contain the address type (Address, BillingAddress,
AlternateBillingAddress)

the location table will also have to have a ‘type’ column

the type column will contain the location type (Location or
MainLocation)

for more info on the type column, check ActiveRecord::Base docs under
Single Table Inheritance.

then just use as follows

company.locations
company.main_location
company.main_location.address
company.billing_address
company_alternate_billing_address

hopefully i understood your dilemma and my comments are useful.
otherwise disregard as the ravings of a lunatic.

Chris.