You should use polymorphic associations as shown below.
# Address model
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
# Customer model
class Customer < ActiveRecord::Base
has_one :address, :as => :addressable
end
# Employee model
class Employee < ActiveRecord::Base
has_one :address, :as => :addressable
end
# Migration script for `addresses` table
class CreateAddresses < ActiveRecord::Migration
def self.up
create_table :addresses, :force => true do |t|
t.references :addressable, :polymorphic => true, :null => false
# add other address fields
t.timestamps
end
add_index :addresses, ["addressable_id", "addressable_type"], :name => "fk_addressable"
end
Now you can do the following:
customer = Customer.new
customer.address = Address.new(:street => "ABC", :city => "SF", :zip => "46646")
customer.save
OR
employee = Employee.last
print employee.address.street