首页 > 解决方案 > Ruby on Rails:未加载种子数据

问题描述

我正在尝试为用户、作品和评论播种数据,但是当我查询数据库时,唯一可用的数据是用户。在为 Works 和 Comments 格式化我的代码时,我做错了什么吗?我正在做一个有很多,通过评论是我的连接表的关系。

{ title:"Guernica", artist: "Pablo Picasso", year: "1937", medium: "oil"},
{ title: "The Birth of Venus", artist: "Sandro Botticelli", year: "1485", medium: "tempera"},
{ title: "The Kiss", artist: "Gustav Klimt", year: "1907", medium:"oil"},
{ title: "American Gothic", artist: "Grant Wood", year: "1930" , medium: "oil"},
{ title: "Nighthawks", artist: "Edward Hopper", year: "1942", medium: "oil"},
{ title: "The Nightwatch", artist: "Rembrandt", year: "1642" , medium: "oil"},
{ title: "Campbell's Soup Cans", artist: "Andy Warhol" , year: "1961" , medium: "syntheic polymer paint"},
{ title: "The Great Wave off Kanagawa", artist: "Hokusai", year: "1820" , medium: "print"},
{ title: "David", artist:"Michelangelo", year: "1501" , medium: "marble"}])


Comment.create({headline:"Comment 1", description: "I think this piece is in France"})
Comment.create({headline:"Comment 2", description: "This is actually a huge work of art in person"})
Comment.create({headline:"Comment 3", description: "Why is this artist known for this piece?"})
Comment.create({headline:"Comment 4", description: "Would this be more iconic if done in another medium?"})
Comment.create({headline:"Comment 5", description: "What does the title mean?"})
Comment.create({headline:"Comment 6", description: "me lol"})
Comment.create({headline:"Comment 7", description: "Game of thrones reference"})
Comment.create({headline:"Comment 8", description: "I could've made that!"})
Comment.create({headline:"Comment 9", description: "what artists were inspired by this piece?"})
Comment.create({headline:"Comment 10", description: "proportions"})'''


Here's my schema: 

create_table "comments", force: :cascade do |t|
    t.string "headline"
    t.string "description"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
    t.integer "work_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "works", force: :cascade do |t|
    t.string "title"
    t.string "artist"
    t.text "year"
    t.string "medium"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end```

标签: ruby-on-railsrubydatabasemigrationdata-migration

解决方案


推荐阅读