Error- ActiveModel::UnknownAttributeError: unknown attribute 'name' for Review

Error- ActiveModel::UnknownAttributeError: unknown attribute 'name' for Review

当我运行:

rails db:seed

在命令行中,出现以下错误:

ActiveModel::UnknownAttributeError: unknown attribute 'name' for Review..........................................................

我在 运行:

时也遇到同样的错误
rails db:seed

seeds.rb

movie = Movie.find_by(title: 'Iron Man')
movie.reviews.create!(name: "Roger Ebert", stars: 3, comment: "I laughed, I cried, I spilled my popcorn!")
movie.reviews.create!(name: "Gene Siskel", stars: 5, comment: "I'm a better reviewer than he is.")
movie.reviews.create!(name: "Peter Travers", stars: 4, comment: "It's been years since a movie superhero was this fierce and this funny.")

movie = Movie.find_by(title: 'Superman')
movie.reviews.create!(name: "Elvis Mitchell", stars: 5, comment: "It's a bird, it's a plane, it's a blockbuster!")


Genre.create!(name: "Action")
Genre.create!(name: "Comedy")
Genre.create!(name: "Drama")
Genre.create!(name: "Romance")
Genre.create!(name: "Thriller")
Genre.create!(name: "Fantasy")
Genre.create!(name: "Documentary")
Genre.create!(name: "Adventure")
Genre.create!(name: "Animation")
Genre.create!(name: "Sci-Fi")

这是我的架构文件[原文如此]:

ActiveRecord::Schema.define(version: 20181218222845) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "characterizations", force: :cascade do |t|
    t.bigint "movie_id"
    t.bigint "genre_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["genre_id"], name: "index_characterizations_on_genre_id"
    t.index ["movie_id"], name: "index_characterizations_on_movie_id"
  end

  create_table "favorites", force: :cascade do |t|
    t.bigint "movie_id"
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["movie_id"], name: "index_favorites_on_movie_id"
    t.index ["user_id"], name: "index_favorites_on_user_id"
  end

  create_table "genres", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "movies", force: :cascade do |t|
    t.string "title"
    t.string "rating"
    t.decimal "total_gross"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "description"
    t.date "released_on"
    t.string "cast"
    t.string "director"
    t.string "duration"
    t.string "image_file_name", default: ""
    t.string "image_content_type"
    t.integer "image_file_size"
    t.datetime "image_updated_at"
    t.string "main_image"
    t.string "slug"
  end

  create_table "reviews", force: :cascade do |t|
    t.integer "stars"
    t.text "comment"
    t.bigint "movie_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.index ["movie_id"], name: "index_reviews_on_movie_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "username"
    t.boolean "admin", default: false
  end

  add_foreign_key "characterizations", "genres"
  add_foreign_key "characterizations", "movies"
  add_foreign_key "favorites", "movies"
  add_foreign_key "favorites", "users"
  add_foreign_key "reviews", "movies"
end

您的 reviews table 没有 name 列,这是 Rails 告诉您的。

我不知道你的应用程序是如何工作的,但我确实在 reviews table 和 users table 上看到了 user_id 列] 有一个 name 列,所以我想您需要使用该名称属性创建一个用户,然后将该用户传递到您正在创建的评论中。

没有看到你的 ReviewUser 模型,我不能肯定地说。

至少,您需要创建一个迁移并 运行 它将 name 字段添加到您的 reviews table:

rails generate migration AddNameToReviews name:string
rails db:migrate

这应该可以解决您报告的错误。