Rails播种许多2个表问题

时间:2016-10-15 09:26:47

标签: ruby-on-rails

我是Rails的新手,并且在播放许多关系表时遇到了问题。这是ERD ERD Diagram

我可以用这个成功创建用户和项目:

user1 = User.create!({
  name: "Mike Rubin",
  userid: "merubin",
  password: "password",
  email:"mike@rubinsworld.com",
  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

我是否需要先以某种方式创建ProjectsPortfio记录并播种用户ID?如果是这样的话? schema and seeds located here on github

谢谢,

麦克

1 个答案:

答案 0 :(得分:0)

我今天早上醒来发现我在用户模型中遗漏了一个has_many:项目给我错误。

我现在可以添加投资组合记录。