Custom inheritance column
suggest changeBy default, an STI model stores its class name in a column named type. Override inheritance_column on the base class to use another column. The value can be a symbol or a string:
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
Admin.create saves the row in users with entity_type set to "Admin". Subclasses must still be loaded before the query, or Rails cannot cast the row back to Admin.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents