How do I seed a belongs_to association?

13,795

Solution 1

What I did was update the particular products to a certain user, see this question:

Can I update all of my products to a specific user when seeding?

Solution 2

Although it sounds like you found a workaround, the solution may be of interested to others.

From your original seeds.rb

user = User.create(:username => 'user', :email => '[email protected]')
store = Store.create(:business_name => 'store', :address => 'Japan')

Create the store

Store.create({
  user_id: user.id
  store_id: store.id
}, without_protection: true)

In the original code snipped "user" and "store" variables are declared. The code assigns user_id / store_id (the model columns inferred by the belongs_to relationship in the Store model) to the id values that are present in the "user" and "store" variables.

"without_protection: true" turns off bulk assignment protection on the id fields. This is perfectly acceptable in a seeds file but should be used with extreme caution when dealing with user provided data.

Solution 3

Or alternatively create your stores.

Then extract the correct one e.g.

store = Store.find_by_business_name('Test Store')

and then create it based on that e.g.

store.products.create(:product_name => "Product Test", :price => '985.93')

This will then set the relationship id for you,

Solution 4

If I'm not mistaken, you're just trying to do this.

user = User.create(:username => 'usertwo', :email => '[email protected]') 
walmart = Store.create(:business_name => 'Walmart', :address => 'San Francisco, USA')

user.products.create(:name => 'Apple', :store => walmart)

Anything else required here that I'm not seeing?

Solution 5

Try doing this

store_1 = Store.new(:business_name => 'Test Store',
:address => 'Test Address',
:phone_number => '555-555-555')

store_1.id = 1
store_1.save!

The trick is not to set the id within the hash as it is protected.

Scott

Share:
13,795

Related videos on Youtube

LearningRoR
Author by

LearningRoR

Updated on June 04, 2022

Comments

  • LearningRoR
    LearningRoR almost 2 years

    I would like to seed my Products and assign them to a specific User and Store.

    Product.rb

    class Product < ActiveRecord::Base
    
      belongs_to :user
      belongs_to :store
    
      def product_store=(id)
        self.store_id = id
      end
    end
    

    Note: Store belongs_to Business (:business_name)

    Seed.rb

    This is my basic setup:

    user = User.create(:username => 'user', :email => '[email protected]') 
    store = Store.create(:business_name => 'store', :address => 'Japan')
    

    I attempted these but they did not work:

    # This gives random ID's ranging from 1 to 4425!?
    user.products.create([{:name => "Apple", :product_store => Store.find_by_address('San Francisco, USA')}])
    
    # This gives me undefined method 'walmart'.
     user.store.products.create([ {:name => "Apple"} ])
    

    Is there a way to set the ID's so I can associate my Products to a Store and User?


    UPDATE -

    I have tried the answers below and still came out unsuccessful. Does anyone know of another way to do this?

    • shadowbq
      shadowbq over 11 years
      You assigned an instance of a class Store to an integer value of store_id .. :product_store => Store.find_by_address('San Francisco, USA'). This would not work.. you should be able to use Store.find_by_address('Japan').id instead
  • LearningRoR
    LearningRoR over 12 years
    I had to change up my entire question. I added more clarity for my scenario. Thank you so far for the help.