title: FactoryGirl association with reference
body: <p>I have four models: <code>User</code>, <code>Product</code>, <code>Ownership</code> and <code>Location</code>. <code>User</code>and <code>Product</code> have one <code>Location</code>, and <code>Location</code> belongs to <code>User</code> and to <code>Product</code> (Polymorphic model).</p>
  
  <p>I want to use <code>FactoryGirl</code> to create products that have the same location as their owner.</p>
  
  
  
  <pre class="lang-rb prettyprint-override"><code>factory :location do
    sequence(:address) { |n| "#{n}, street, city" }
  end
  
  factory :user do
    sequence(:name)  { |n| "Robot #{n}" }
    sequence(:email) { |n| "numero#{n}@robots.com"}
    association :location, factory: :location
  end
  
  factory :product do
    sequence(:name) { |n| "Objet #{n}" }
    association :location, factory: :location
  end
  
  factory :ownership do
    association :user, factory: :user
    association :product, factory: :product
  end
  </code></pre>
  
  <p>I created a method in the <strong>product model file</strong> to retrieve the product's owner just by doing <code>product.owner</code>. </p>
  
  <p>I want to adapt the <strong>product factory</strong> in order to replace the factoried location by <code>product.owner.location</code>. How can I do that?</p>
  
  <h2>EDIT 1</h2>
  
  <p>I want to use it like that:</p>
  
  <p>First I create a user </p>
  
  <pre class="lang-rb prettyprint-override"><code>FactoryGirl.create(:user)
  </code></pre>
  
  <p>Later I create a product</p>
  
  <pre class="lang-rb prettyprint-override"><code>FactoryGirl.create(:product)
  </code></pre>
  
  <p>When I associate both of them </p>
  
  <pre class="lang-rb prettyprint-override"><code>FactoryGirl.create(:current_ownership, product: product, user: user)
  </code></pre>
  
  <p>I want that the location of my product becomes the one of his owner.</p>