无效的单表继承类型:Rails

时间:2013-12-03 17:40:16

标签: ruby-on-rails inheritance ruby-on-rails-4

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

  

无效的单表继承类型:Movie不是其子类   产品

我该如何解决这个问题?

enter image description here

控制器

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

4 个答案:

答案 0 :(得分:91)

您不应使用type关键字作为列名,因为它是ActiveRecord的保留字


但如果您真的想使用它,无论出于何种原因(例如,如果您无法控制数据库结构),您应该采取以下措施:

首先,确保您的电影模型继承自(false - )&#34; abstract&#34;型号产品:

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方法并在产品的常量中添加类型:

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

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

答案 1 :(得分:27)

如果您不打算为'type'创建值的模型,那么可能发生的是'type'是ActiveRecord中的保留字。

请参阅http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord/Naming

type - 仅在具有单表继承且必须包含类名”

时使用

答案 2 :(得分:20)

self.inheritance_column = nil添加到您的模型中。类型是保留的。

答案 3 :(得分:0)

另一种方法是以不同的方式命名列(product_type在这里没问题),并在模型定义中添加属性别名alias_attribute :type, :product_type

这样您就可以安全地使用@product.type,因为当您阅读或写入product_type属性时,Rails会替换type

使用Rails 4.1.2进行测试。