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?
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
Single-table inheritance (STI) is the practice of storing multiple types of values in the same table, where each record includes a field indicating its type, and the table includes a column for every field of all the types it stores.
Second and more important, you need to have one column per attribute on any subclass, and any attribute that is not shared by all the subclasses must accept nil values.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With