Single Table Inheritance
Using subclasses is a common method of achieving reuse in object-oriented software. Rails provides a mechanism for storing instances of different classes in the same table, called Single Table Inheritance. Rails will take care of most of the details, writing the classâs name to the type column and instantiating the correct class when results come back from the database.
Symptoms
- You need to change from one subclass to another.
- Behavior is shared among some subclasses but not others.
- One subclass is a fusion of one or more other subclasses.
Solutions
- If youâre using STI to reuse common behavior, use Replace Subclasses with Strategiesâ to switch to a composition-based model.
- If youâre using STI so that you can easily refer to several different classes in the same table, switch to using a polymorphic association instead.
Prevention
- Following Composition Over Inheritanceâ