Prevent STI when inheriting from an ActiveRecord model

Mike picture Mike · Jul 26, 2012 · Viewed 9.6k times · Source

On Rails 3.2.6, I have a class that inherits from ActiveRecord::Base:

class Section < ActiveRecord::Base
  ...
end

When I inherit from this class, Rails will assume I want STI:

class AnotherSection < Section
   ..Rails assumes I have a type field, etc...
end

I want to be able to inherit from the Section class and use the subclass as a normal Ruby subclass, without the Rails STI magic.

Is there a way to prevent STI when subclassing from an ActiveRecord::Base model?

Answer

Veraticus picture Veraticus · Jul 26, 2012

You can achieve this by disabling the inheritance_column for the model, like so:

class AnotherSection < Section
  # disable STI
  self.inheritance_column = :_type_disabled

end