ruby-on-railsactiverecordsingle-table-inheritance

Rails / ActiveRecord - Single Table Inheritance - overriding type field


Is it possible to override the name of this colummn? I'm changing some parts of my applications to use STI and there are other fields in use for. I would also prefer it to be of type integer.

Any ideas?


Solution

  • In modern Rails, you'd use inheritance_column= (as panckreous noted):

    class M < ApplicationRecord
      self.inheritance_column = 'whatever'
      #...
    end
    

    In older versions of Rails (i.e. what was around when this answer was originally written), you'd use [set_inheritance_column] to change the name:

    Sets the name of the inheritance column to use to the given value, or (if the value is nil or false) to the value returned by the given block.

    The column still has to be a string (or text) as AR will want to put the class name in there:

    Single table inheritance

    Active Record allows inheritance by storing the name of the class in a column that is named “type” by default.