【问题标题】:Seed error ActiveModel::UnknownAttributeError: unknown attribute 'excerpt' for List种子错误 ActiveModel::UnknownAttributeError: List 的未知属性“摘录”
【发布时间】:2018-10-14 15:18:11
【问题描述】:

我正在设置我的种子文件以向我的 Rails 5 应用程序添加一些数据。 当我运行 rake db:seed 时,我收到了这个错误

rake aborted!
ActiveModel::UnknownAttributeError: unknown attribute 'excerpt' for List.

无法真正理解为什么我会得到这个。我使用 PostgreSQL 作为 DB 和 Rails 5.2。我正在尝试构建一个 API。

我设置了数据库进行了迁移,但种子不起作用。

我还有另外 2 个模型和一个用于物品的控制器。

种子文件:

List.create(title:"West Sweden Road Trip", excerpt:"A cool road trip with stops in harbors of the coast")

我的模型是:

class List < ApplicationRecord
  has_many :list_items
  has_many :items, through: :list_items
end

控制器:

module Api::V1
  class ListsController < ApplicationController
    before_action :set_list, only: [:show, :update, :destroy]

    # GET /lists
    def index
      @lists = List.order(:id)

      render json: @lists
    end

    # GET /lists/1
    def show
      render json: @list
    end

    # POST /lists
    def create
      @list = List.new(list_params)

      if @list.save
        render json: @list, status: :created
      else
        render json: @list.errors, status: :unprocessable_entity
      end
    end

    # PATCH/PUT /lists/1
    def update
      if @list.update(list_params)
        render json: @list
      else
        render json: @list.errors, status: :unprocessable_entity
      end
    end

    # DELETE /lists/1
    def destroy
      @list.destroy
      if @list.destroy
        head :no_content, status: :ok
      else
        render json: @list.errors, status: :unprocessable_entity
      end
    end

    private
    # Use callbacks to share common setup or constraints between actions.
    def set_list
      @list = List.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def list_params
      params.require(:list).permit(:title, :excerpt, :description, :upvotes)
    end
  end
end

我的架构:

ActiveRecord::Schema.define(version: 2018_10_14_135801) do

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

  create_table "items", force: :cascade do |t|
    t.integer "type"
    t.string "name"
    t.text "excerpt"
    t.text "description"
    t.string "url"
    t.integer "upvotes"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "list_items", force: :cascade do |t|
    t.bigint "list_id"
    t.bigint "item_id"
    t.text "description"
    t.integer "position"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["item_id"], name: "index_list_items_on_item_id"
    t.index ["list_id"], name: "index_list_items_on_list_id"
  end

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

  add_foreign_key "list_items", "items"
  add_foreign_key "list_items", "lists"
end

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

    rake 中止! ActiveModel::UnknownAttributeError: 未知属性 列表的“摘录”。

    好吧,List 没有一个名为 excerpt 的属性,但 Itemexcerpt 属性

    改成

    List.create(title:"West Sweden Road Trip")
    

    更新:

    如果您想将excerpt 列添加到List,则生成迁移

    rails g migration add_excerpt_to_lists excerpt:text
    

    然后做rake db:migrate

    【讨论】:

    • 没错,但我正在按照本教程进行操作medium.com/@bruno_boehm/… 也许那是错误的,或者我错过了什么?
    • @Jakub 教程是对的!在教程中,作者创建了带有excerpt 列的List 模型。看到这个rails g scaffold List title:string excerpt:text description:text upvotes:integer
    • 好吧,我错过了。我可以以某种方式手动添加摘录以避免此错误吗? @帕万
    • @Jakub 您需要为此生成迁移。检查我的更新
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多