2011-10-31 3 views
0

나는이 내 seeds.rb 파일에 다음과 같은 데이터 :사용자를 특정 시드 데이터에 어떻게 할당 할 수 있습니까?

users = User.create([ 
    { 
     :email => '[email protected]', 
     :password => 'test', 
     :password_confirmation => 'test' 
    }, 
    { 
     :email => '[email protected]', 
     :password => 'test', 
     :password_confirmation => 'test' 
    } 
    ]) 
puts 'Users added' 

UserPrice.create([ 
    { 
    # assign user 1 
    :product_name => "Great Value Vitamin D Whole Milk", 
    :price => '3.81', 
    :purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"), 
    :store => "New York"}, 
    { 
    #assign user 2 
    :product_name => 'Eggs', 
    :price => '2.78', 
    :purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"), 
    :store => "New York" 
    } 
]) 
puts 'Added Prices' 

어떻게 내 seeds.rb에 UserPrices에 정당한 사용자를 할당합니까?

참고 : :user => users.first을 시도했지만 작동하지 않았습니다.


작업 코드 :

user1 = User.create(:email => '[email protected]', :password => 'qweasd', :password_confirmation => 'qweasd') 
user2 = User.create(:email => '[email protected]',:password => 'qweasd',:password_confirmation => 'qweasd') 

user1.user_prices.create(
    :product_name => "Great Value Vitamin D Whole Milk", 
    :price => '3.81', 
    :purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"), 
    :store => "New York" 
    ) 
user2.user_prices.create(
    :product_name => 'Eggs', 
    :price => '2.78', 
    :purchase_date => Date.strptime("08/25/2011", "%m/%d/%Y"), 
    :store => "New York" 
    ) 

답변

1

당신은이 라인을 따라이 더 많은 작업을 수행 할 수 있습니다 :

user = User.create(#stuff#) 
user.user_prices.create(#stuff#) 

가 has_many 관계를 가정.

관련 문제