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"
)