Ruby on Rails Single Table Inheritance Custom inheritance column


Example

By default STI model class name is stored in a column named type. But its name can be changed by overriding inheritance_column value in a base class. E.g.:

class User < ActiveRecord::Base
  self.inheritance_column = :entity_type # can be string as well
end

class Admin < User; end

Migration in this case will look as follows:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :password
      t.string :entity_type

      t.timestamps
    end
  end
end

When you do Admin.create, this record will be saved in the users table with entity_type = "Admin"