我在try 在Rails应用程序中创建新产品时遇到此错误.

无效的单表继承类型:Movie不是

我该如何解决这个问题?

在此处输入图像描述

控制器

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render action: 'show', status: :created, location: @product }
      else
        format.html { render action: 'new' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :artist, :type, :release_date, :category, :publisher, :format, :description, :sale_price, :rental_price)
    end
end

迁移

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :title
      t.string :artist
      t.string :type
      t.date :release_date
      t.string :category
      t.string :publisher
      t.string :format
      t.text :description
      t.decimal :sale_price
      t.decimal :rental_price

      t.timestamps
    end
  end
end

推荐答案

You should not use the 100 keyword作为列名,因为it is a reserved word for ActiveRecord.


但如果你真的想使用它,出于任何原因(比如如果你不能控制数据库 struct ),你应该做以下几点:

首先,确保您的电影模型继承自(假-"抽象"模型产品:

class Product < ActiveRecord::Base
  TYPES = %w( Movie )
  before_save :set_type
  validates :type, presence: true, :inclusion => { :in => TYPES }

  def set_type
    raiser "You must override this method in each model inheriting from Product!"
  end

  # ...

class Movie < Product

  def set_type # If you don't implement this method, an error will be raised
    self.type = 'Movie'
  end

然后在ProductsController中,您可以管理(CRUD)所有类型的产品.


To add a new type of product:您只需定义一个从产品继承的新模型,实现它的set_type方法,并将类型添加到产品的常量中:

class Book < Product
  def set_type
    self.type = 'Book'
  end
  #...

class Product < ActiveRecord::Base
  TYPES = %w( Movie Book )

Ruby-on-rails相关问答推荐

使用Hotwire/Turbo的Rails在链接悬停时获取请求

Django模型在关系更新时更新相关对象

发送不调用类或模块中的私有方法

Rails 使用多个外键创建/构建

Rails 7 - has_many_attached 在加载新附件时删除旧附件

ArgumentError 用于 Ruby on Rails 中非常简单的初始化方法,没有参数

Rails 'includes' 和 'where' 与 belongs_to 和 has_many 关联

正确更新其他列的更新列

如何在 Windows 中更新 ruby

PostgreSQL 无间隙序列

Heroku 错误 R14(超出内存配额):我该如何解决?

Ruby on Rails:有条件地显示部分

Rails 3 中的猴子补丁

Ruby on Rails:在模型中验证还是在数据库中验证更好?

如何从邮箱中获取域

如何让 ActiveAdmin 使用强参数?

Rails - 具有空数组的强参数

link_to 将参数与 url 一起发送并在目标页面上抓取它们

Rails 3 设计,current_user 在模型中不可访问?

无论何时 gem 在 Heroku 上运行 cron 作业(job)