0
私は私のseeds.rb
ファイルに次のデータを持っている:私は私のseeds.rbにUserPrices
に正当なユーザーを割り当てるにはどうすればよい特定のシードデータにユーザーを割り当てる方法を教えてください。
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'
を?
注::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"
)