💎 Ruby on Rails
Intermediate
What is Single Table Inheritance (STI) in Rails?
Answer
Single Table Inheritance (STI) allows multiple model classes to share a single database table, distinguished by a type column. Example: Employee, Manager, and Developer all inherit from Person and use the people table. Rails stores the class name in the type column. When querying Manager.all, Rails adds WHERE type = 'Manager'. STI works well when subclasses share most attributes. Drawbacks: sparse columns (fields unused by some subclasses), can lead to a wide table. An alternative is polymorphic associations or separate tables.
Previous
What is the difference between dependent: :destroy and dependent: :delete_all?
Next
What is ActionCable in Rails?