STI Stands for Single Table Inheritance

// Apr 29, 2015

Today’s topic that peaked my inheritance was Single Table Inheritance, which is having multiple models inherit their attributes from a base model. In Rails, STI occurs when all your models inherit from ActiveRecord::Base.

So in what situation would we best benefit from STI? Well when a model in your database as many types of itself. The best example I can think of is a User model. Imagine there are many “type” of users, such as:


Users

  • Guests
  • Admin
  • Verified
  • Banned


In a case such as this, STI is perfect because all of these sub-categories are their own objects with their own attributes and abilities, however they’re all nested under the User model.

First you would need to add a :type column as a string to the User model in migrations, and Rails will automagically specify that column with the sub-category model name depending on what type of User was created or updated.

class AddTypeToUsers < ActiveRecord::Migration
  def change
    add_column :users, :type, :string
  end
end


Now whenever you create the Admin or Guest or whatever model; you simply need to make those models inherit from User which is subsequently inherit from ActiveRecord::Base.

Just like that, you now have multiply types of users whom can also all have their own unique attributes and permissions!