gpt4 book ai didi

ruby-on-rails - 无效的单表继承类型:Rails

转载 作者:行者123 更新时间:2023-12-03 08:50:05 25 4
gpt4 key购买 nike

尝试在Rails应用程序中创建新产品时出现此错误。

Invalid single-table inheritance type: Movie is not a subclass of Product



我该如何解决?

控制者
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

最佳答案

不应将type关键字作为列名,因为是ActiveRecord 的保留字。

但是,如果出于任何原因(例如,如果您无法控制数据库结构),您确实要使用它,则应该执行以下操作:

首先,确保您的Movie模型继承自(false-)“抽象”模型产品:

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)所有类型的产品。

要添加新的产品类型:,您只需要定义一个继承自Product的新模型,实现它的set_type方法,然后在产品的Constant中添加类型:
class Book < Product
def set_type
self.type = 'Book'
end
#...

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

关于ruby-on-rails - 无效的单表继承类型:Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com