Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assume you are using Rails 3.</p> <p>You should use <code>habtm</code> when your join table has only 2 foreign keys. For most cases, <code>has_many :through</code> will be more flexible.</p> <p>In your case, you should create a model for the join table. First, you should disable pluralization for legacy DB schema. (I thought you already did that). </p> <pre><code># In config/application.rb config.active_record.pluralize_table_names = false </code></pre> <p>Create a join table, namely <code>CustomerPhoneAddress</code> by convention (I will give it a more meaningful name):</p> <pre><code># customer_phone_address.rb class CustomerPhoneAddress &lt; ActiveRecord::Base belongs_to :address_master belongs_to :customer_master belongs_to :phone_master end </code></pre> <p>Finally, associate your models with <code>has_many</code> and <code>has_many :through</code>:</p> <pre><code># address_master.rb class AddressMaster &lt; ActiveRecord::Base has_many :customer_phone_addresses has_many :customer_masters, :through =&gt; :customer_phone_addresses has_many :phone_masters, :through =&gt; :customer_phone_addresses end # customer_master.rb class CustomerMaster &lt; ActiveRecord::Base has_many :customer_phone_addresses has_many :address_masters, :through =&gt; :customer_phone_addresses has_many :phone_masters, :through =&gt; :customer_phone_addresses end # phone_master.rb class PhoneMaster &lt; ActiveRecord::Base has_many :customer_phone_addresses has_many :customer_masters, :through =&gt; :customer_phone_addresses has_many :phone_masters, :through =&gt; :customer_phone_addresses end </code></pre> <p>I tested the code and it works very well. To create an association:</p> <pre><code>CustomerPhoneAddress.create( :phone_master =&gt; PhoneMaster.first, :address_master =&gt; AddressMaster.first, :customer_master =&gt; CustomerMaster.first) </code></pre> <p>To query the association:</p> <pre><code>IRB&gt; a = AddressMaster.first =&gt; #&lt;AddressMaster id: 1, created_at: "2011-12-07 15:23:07", updated_at: "2011-12-07 15:23:07"&gt; IRB&gt; a.customer_masters =&gt; [#&lt;CustomerMaster id: 1, created_at: "2011-12-07 15:23:15", updated_at: "2011-12-07 15:23:15"&gt;] IRB&gt; a.phone_masters =&gt; [#&lt;PhoneMaster id: 1, created_at: "2011-12-07 15:23:19", updated_at: "2011-12-07 15:23:19"&gt;] IRB&gt; a.customer_phone_addresses =&gt; [#&lt;CustomerPhoneAddress id: 1, address_master_id: 1, customer_master_id: 1, phone_master_id: 1, created_at: "2011-12-07 15:24:01", updated_at: "2011-12-07 15:24:01"&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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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