2016-10-15 25 views
0

我是Rails的新手,並且遇到播種許多2個關係表的問題。這裏是ERD ERD DiagramRails播種許多2個表格問題

我可以成功地創建用戶和項目與此:

user1 = User.create!({ 
    name: "Mike Rubin", 
    userid: "merubin", 
    password: "password", 
    email:"[email protected]", 
    avatar_url:"http://mike-rubin.com/images/nav/id/rubin_selfie_270.png" 
}) 

project1 = user1.projects.create!({ 
name:"Mario's Match Game", 
header1:"Javascript Project", 
header2:"", 
description:"Pre-work project for WDI-DC12 Bootcamp. Concentration like game where random cards are displayed and flipped as user choices. This work includes CSS, HTML5 along with Javascript and dynamic DOM manulipuation. 
Bonus work to keep score and win and loss was done.", 
screen_img_url:"http://mike-rubin.com/images/portfolio/mariomemorygame.jpg", 
host_url:"http://www.rubinsworld.com/ga/memory/index.html" 
}) 

如果我嘗試創建一個投資組合是:

portfolio1 = project1.portfolios.create!({ 
title:"Mike Rubin's Personal Portfolio", 
description:"Welcome to Mike Rubin's Web Development Portfolio. " 
}) 

我得到這個錯誤:

$ rails db:seed 
rails aborted! 
ActiveRecord::RecordInvalid: Validation failed: User must exist 

我是否需要以某種方式創建ProjectsPortf首先記錄並播種用戶ID?如果是的話如何? schema and seeds located here on github

感謝,

邁克

+0

你可以將模型本身添加到你的問題 – David

+0

如果你想調試建議你需要添加一個[MVCE](http://stackoverflow.com/help/mcve)的問題,因爲我們都沒有一個水晶球我們可以使用它來窺探你的代碼。 – max

回答

0

我今天早上醒來發現我已經離開了的has_many:在這是給我的錯誤的用戶樣板工程。

我現在可以添加投資組合記錄。