The documentation for ActiveRecord associations has a section on has_one
vs belongs_to
. 또한 section on 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">
>>
테이블 디자인이 도움이 될 것입니다. 나는 .references "데이터 유형"을 인식하지 못했습니다. – y0mbo