recursive_acronym
recursive_acronym

Reputation: 3031

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?

Upvotes: 1

Views: 2746

Answers (2)

mu is too short
mu is too short

Reputation: 434665

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.

Upvotes: 5

panckreous
panckreous

Reputation: 527

According to the code in ActiveRecord::ModelSchema (3.2), the set_inheritance_column method is now deprecated and you should use self.inheritance_column = column

Upvotes: 7

Related Questions