web-dev-qa-db-ja.com

無効な単一テーブル継承タイプ:Rails

Railsアプリケーションで新しい製品を作成しようとすると、このエラーが発生します。

単一テーブルの継承タイプが無効です:映画は製品のサブクラスではありません

どうすれば解決できますか?

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
31
Antarr Byrd

列名としてtypeキーワードを使用しないでください。ActiveRecordの予約語であるためです


しかし、何らかの理由で(DB構造を制御できない場合など)本当に使用したい場合は、次のようにする必要があります。

まず、Movieモデルが(false-) "abstract"モデルを継承していることを確認してくださいProduct:

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)できます。


製品の新しいタイプを追加するには、製品から継承する新しいモデルを定義するだけで、そのset_typeメソッドを実装し、製品の定数にタイプを追加します。 :

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

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

「type」の値のモデルを作成するつもりがない場合、「type」はActiveRecordの予約語です。

http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord/Naming を参照してください

type-これは、単一のテーブル継承があり、クラス名を含める必要がある場合にのみ使用されます」

27
h4xnoodle

追加 self.inheritance_column = nilモデルに。タイプは予約されています。

21
DDDD

別の方法は、列に異なる名前を付けることです(product_typeはここで問題ありません)、属性エイリアスを追加しますalias_attribute :type, :product_typeモデル定義で。

そうすれば、@product.type安全に、Railsはproduct_typetype属性の読み取りまたは書き込み時。

Rails 4.1.2。

0
Goulven