Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you want an address to belong both to a company and to a contact, you have two choices. If you aren't going to ever need multiple addresses (which I might recommend against assuming), then you can put a "belongs_to :address" in both Company and Contact. Address would then have both a "has_many :companies" and "has_many :contacts". </p> <p>The better option that makes more sense is to use polymorphic associations, where the address table would have :addressable_type and :addressable_id columns to link back to either model. </p> <pre><code>class Company &lt; ActiveRecord::Base attr_accessible :name, :phone, :addresses has_many :contacts accepts_nested_attributes_for :contacts has_many :addresses, :as =&gt; :addressable accepts_nested_attributes_for :addresses end class Contact &lt; ActiveRecord::Base attr_accessible :first_name, :last_name, :title, :phone, :fax, :email, :company, :date_entered, :campaign_id, :company_name belongs_to :company has_many :addresses, :as =&gt; :addressable accepts_nested_attributes_for :addresses end class Address &lt; ActiveRecord::Base attr_accessible :street1 belongs_to :addressable, :polymorphic =&gt; true end </code></pre> <p>Now your trouble comes with wanting to have a single association "company.addresses" to get both it's address and those of its contacts. I might recommend staying away from this and using a different approach.</p> <p>You're going to want to map those addresses in the view to the correct record, so it might be best to separate them:</p> <pre><code>&lt;% form_for @company do |company_form| %&gt; &lt;% company_form.fields_for :addresses do |address_form| %&gt; &lt;%= address_form.text_field :street1 %&gt; &lt;% end %&gt; &lt;% company_form.fields_for :contacts do |contact_form| %&gt; &lt;% contact_form.fields_for :addresses do |contact_address_form| %&gt; &lt;%= contact_address_form.text_field :street1 %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Your address table has the polymorphic columns like this:</p> <pre><code>class CreateAddresses &lt; ActiveRecord::Migration def self.up create_table :addresses do |t| t.string :street1 t.string :addressable_type t.integer :addressable_id t.timestamps end end def self.down drop_table :addresses end end </code></pre> <p>I'm not sure how you could keep all the linking straight if you don't nest the records individually like this.</p> <p>If you do need to get all the addresses for a company for display purposes, you could use the standard ruby enumerable manipulations like collect to gather them together.</p> <p>Good luck!</p>
    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.
 

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