Invalid single-table inheritance type: Rails

You should not use the type keyword as the column name because it is a reserved word for ActiveRecord.


But if you really want to use it, for any reason (like if you don’t have control on the DB structure), here is what you should do:

First, make sure your Movie model inherits from the (false-)”abstract” model 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

And then in your ProductsController you can manage (CRUD) all kind of products.


To add a new type of product: you just have to define a new Model inheriting from Product, implement it’s set_type method and add the type in the product’s Constant:

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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)