title: How do I seed a belongs_to association?
body: <p>I would like to seed my <code>Products</code> and assign them to a specific <code>User</code> and <code>Store</code>.</p>
  
  <p><strong>Product.rb</strong></p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
  
    belongs_to :user
    belongs_to :store
  
    def product_store=(id)
      self.store_id = id
    end
  end
  </code></pre>
  
  <p>Note: <code>Store</code> belongs_to <code>Business</code> (<code>:business_name</code>)</p>
  
  <p><strong>Seed.rb</strong></p>
  
  <p>This is my basic setup:</p>
  
  <pre><code>user = User.create(:username =&gt; 'user', :email =&gt; 'user2@email.com') 
  store = Store.create(:business_name =&gt; 'store', :address =&gt; 'Japan')
  </code></pre>
  
  <hr>
  
  <p>I attempted these but they did not work:</p>
  
  <pre><code># This gives random ID's ranging from 1 to 4425!?
  user.products.create([{:name =&gt; "Apple", :product_store =&gt; Store.find_by_address('San Francisco, USA')}])
  
  # This gives me undefined method 'walmart'.
   user.store.products.create([ {:name =&gt; "Apple"} ])
  </code></pre>
  
  <p>Is there a way to set the ID's so I can associate my <code>Products</code> to a <code>Store</code> and <code>User</code>?</p>
  
  <hr>
  
  <p><strong>UPDATE -</strong></p>
  
  <p>I have tried the answers below and still came out unsuccessful. Does anyone know of another way to do this?</p>