В документации для ассоциаций ActiveRecord есть раздел has_one
против belongs_to
. Кроме того, в разделе в has_one
упоминается, что его следует использовать, только если другой класс имеет внешний ключ. Итак, для моделирования того, что вы хотите, вы бы использовали.
class Address < ActiveRecord::Base
has_one :shipto_customer, :class_name => "Customer", :foreign_key => "shipping_address_id"
has_one :billto_customer, :class_name => "Customer", :foreign_key => "billing_address_id"
end
class Customer < ActiveRecord::Base
belongs_to :shipping_address, :class_name => "Address"
belongs_to :billing_address, :class_name => "Address"
end
Пример использования:
>> customer = Customer.new(:name => "John Smith",
?> :shipping_address => Address.new(:address => "123 M St",
?> :city => "Phoenix", :state => "AZ", :zip => "85015"),
?> :billing_address => Address.new(:address => "555 W Main Dr",
?> :city => "Phoenix", :state => "AZ", :zip => "85015")
>> )
=> #<Customer id: nil, name: "John Smith", billing_address_id: nil, shipping_address_id: nil, created_at: nil, updated_at: nil>
>> customer.save
Address Create (0.8ms) INSERT INTO "addresses" ("address", "city", "zip", "created_at", "updated_at", "state") VALUES('555 W Main Dr', 'Phoenix', '85015', '2009-11-14 17:03:28', '2009-11-14 17:03:28', 'AZ')
Address Create (0.2ms) INSERT INTO "addresses" ("address", "city", "zip", "created_at", "updated_at", "state") VALUES('123 M St', 'Phoenix', '85015', '2009-11-14 17:03:28', '2009-11-14 17:03:28', 'AZ')
Customer Create (0.2ms) INSERT INTO "customers" ("name", "billing_address_id", "shipping_address_id", "created_at", "updated_at") VALUES('John Smith', 1, 2, '2009-11-14 17:03:28', '2009-11-14 17:03:28')
=> true
>> customer.shipping_address
=> #<Address id: 2, address: "123 M St", city: "Phoenix", state: "AZ", zip: "85015", created_at: "2009-11-14 17:03:28", updated_at: "2009-11-14 17:03:28">
>> customer.billing_address
=> #<Address id: 1, address: "555 W Main Dr", city: "Phoenix", state: "AZ", zip: "85015", created_at: "2009-11-14 17:03:28", updated_at: "2009-11-14 17:03:28">
>>