Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html" rel="nofollow noreferrer">The documentation for ActiveRecord associations has a section on <code>has_one</code> vs <code>belongs_to</code>.</a> In addition, the <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001834" rel="nofollow noreferrer">section on <code>has_one</code></a> mentions that this should be used only if the <em>other class</em> has the foreign key. So, to model what you want, you would use.</p> <pre><code>class Address &lt; ActiveRecord::Base has_one :shipto_customer, :class_name =&gt; "Customer", :foreign_key =&gt; "shipping_address_id" has_one :billto_customer, :class_name =&gt; "Customer", :foreign_key =&gt; "billing_address_id" end class Customer &lt; ActiveRecord::Base belongs_to :shipping_address, :class_name =&gt; "Address" belongs_to :billing_address, :class_name =&gt; "Address" end </code></pre> <p>Example usage:</p> <pre><code>&gt;&gt; customer = Customer.new(:name =&gt; "John Smith", ?&gt; :shipping_address =&gt; Address.new(:address =&gt; "123 M St", ?&gt; :city =&gt; "Phoenix", :state =&gt; "AZ", :zip =&gt; "85015"), ?&gt; :billing_address =&gt; Address.new(:address =&gt; "555 W Main Dr", ?&gt; :city =&gt; "Phoenix", :state =&gt; "AZ", :zip =&gt; "85015") &gt;&gt; ) =&gt; #&lt;Customer id: nil, name: "John Smith", billing_address_id: nil, shipping_address_id: nil, created_at: nil, updated_at: nil&gt; &gt;&gt; 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') =&gt; true &gt;&gt; customer.shipping_address =&gt; #&lt;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"&gt; &gt;&gt; customer.billing_address =&gt; #&lt;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"&gt; &gt;&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload