title: Factory Girl with polymorphic association for has_many and has_one
body: <p>I am currently working on a project and I wanted to create tests using factory girl but I'm unable to make it work with polymorphic has_many association. I've tried many different possibilities mentioned in other articles but it still doesn't work. My model looks like this:</p>
  
  <pre><code>class Restaurant &lt; ActiveRecord::Base
    has_one :address, as: :addressable, dependent: :destroy
    has_many :contacts, as: :contactable, dependent: :destroy
  
    accepts_nested_attributes_for :contacts, allow_destroy: true
    accepts_nested_attributes_for :address, allow_destroy: true
  
    validates :name, presence: true
    #validates :address, presence: true
    #validates :contacts, presence: true
  end
  
  class Address &lt; ActiveRecord::Base
    belongs_to :addressable, polymorphic: true
  
    # other unimportant validations, address is created valid, the problem is not here
  end
  
  class Contact &lt; ActiveRecord::Base
    belongs_to :contactable, polymorphic: true
    # validations ommitted, contacts are created valid
  end
  </code></pre>
  
  <p>So bassically I want to create factory for Restaurant with address and contacts (with validations on Restaurant for presence, but if it's not possible, even without them) but I'm unable to do so. Final syntax should be like:</p>
  
  <pre><code>let(:restaurant) { FactoryGirl.create(:restaurant) }
  </code></pre>
  
  <p>Which should also create associated address and contacts. I have read many articles but I always get some sort of error. Currently my factories (sequences are defined correctly) are like this:</p>
  
  <pre><code>factory :restaurant do
    name
    # address {FactoryGirl.create(:address, addressable: aaa)}
    # contacts {FactoryGirl.create_list(:contact,4, contactable: aaa)}
    # Validations are off, so this callback is possible
    after(:create) do |rest|
      # Rest has ID here
      rest.address = create(:restaurant_address, addressable: rest)
      rest.contacts = create_list(:contact,4, contactable: rest)
    end
  end
  
  factory :restaurant_address, class: Address do
    # other attributes filled from sequences...
  
    association :addressable, factory: :restaurant
    # addressable factory: restaurant
    # association(:restaurant)
  end
  
  factory :contact do
    contact_type
    value
  
    association :contactable, :factory =&gt; :restaurant
  end
  </code></pre>
  
  <p>Is there a way to create restaurant with one command in test with addresses and contacts set? Do I really need to get rid off my validations because of after(:create) callback? 
  Current state is as fllowing:</p>
  
  <ol>
  <li>Restaurant is created with name and id.</li>
  <li>Than the address is being created - all is correcct, it has all the values including addressable_id and addressable_type</li>
  <li>After that all contacts are being creaed, again everything is fine, cntacts has the right values.</li>
  <li>After that, restaurant doesn't have any ids from associated objects, no association to adddress or contacts</li>
  <li>After than, for some reason restaurant is build again (maybe to add those associations?) and it fails: I get ActiveRecord:RecordInvalid. </li>
  </ol>
  
  <p>I'm using factory_girl_rails 4.3.0, ruby 1.9.3 and rails 4.0.1. I will be glad for any help.</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Just for clarification of my goal, I want to be able to create restaurant in my spec using one command and be able to access associated address and contacts, which should be created upon creation of restaurant. Let's ignore all validations (I had them commented out in my example from the beginning). When I use <strong>after(:build)</strong>, first restaurant is created, than address is created with restaurant's ID as <strong>addressable_id</strong> and class name as <strong>addressable_type</strong>. Same goes to contacts, all are correct. The problem is, that restaurant doesn't know about them (it has no IDs of address or contacts), I can't access them from restaurant which I want to. </p>