2011-10-31 12 views
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" 
    ) 

答えて

1

あなたはこれらの線に沿ってこれ以上やってみたいことがあります

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

はhas_manyの関係を仮定。

関連する問題