Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent STI when inheriting from an ActiveRecord model

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?

like image 732
Mike Avatar asked Jul 26 '12 19:07

Mike


People also ask

What does ActiveRecord base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is STI in Ruby?

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.

What is a standard prerequisite for implementing single table inheritance?

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.


1 Answers

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 
like image 148
Veraticus Avatar answered Oct 05 '22 16:10

Veraticus