How Single Table Inheritance Works in Ruby

0
1827

On Tuesdays I help you prepare for coding job interviews and today I’m going to discuss how single table inheritance works. I picked single table inheritance as a topic because I was specifically asked about it in the last programming interview that I had.

As an initial note, single table inheritance is commonly referred to with the acronym STI. Some common questions related to single table inheritance are:

  • How does single table inheritance work?
  • When is a good time to use STI?
  • How do you implement single table inheritance?

Simple Explanation of Single Table Inheritance

Before we walk through how to implement STI in an application, let’s discuss what it is at a high level. Dead simply definition:

Single table inheritance is a technique that enables developers to extend a data model without having to create another database table.

Don’t worry if that is clear as mud. We’ll walk through a practical example of how it works that should clarify the concept of STI.

Single Table Inheritance Tutorial

The Challenge

Single Table Inheritance

Let’s imagine that you’re building a social networking application. In the application you have two types of users: standard users and admin users. If you were to create a database table for both of the user types it would look something like this.

This would be a very poor way to build an application. As you can see, the attributes and methods are very similar. This means that each time that you make a change to the users in the application you’ll most likely need to make changes to both database tables. This type of code design will eventually lead to a project that will be difficult to manage.

At its core, this design breaks the Don’t Repeat Yourself (DRY) principle that is vital in Object Oriented Programming development.

The Solution

But don’t worry, Single Table Inheritance is here to save our coding sins. STI let’s us create an abstract class that we can call User.

Single Table Inheritance

From that point we can create a subclass of our User class called StandardUser.

Single Table Inheritance

As you can see our StandardUser only contains the attributes and methods that are specific to regular users. This is possible by leveraging Object Oriented Inheritancewhich I discussed a few weeks ago. If you review the principles of inheritance you’ll remember that inherited classes have access to the data and behavior of their parent classes. In our example this means that the StandardUser class will have all of the attributes, such as first_name and password, even though it’s not declared in the class itself.

Now we can create another subclass of User called AdminUser that will contain the data and attributes specific to admins.

Single Table Inheritance

A Key Requirement

Single Table Inheritance

One of the keys for single table inheritance to work lies in the type attribute in the parent User class. If we would create an admin user using code such as:

Single Table Inheritance

It will create a record in the User database table that looks something like this:

Single Table Inheritance

Notice how the AdminUser value automatically got inserted in the type column? This is how the User class table knows what child class the user actually belongs to.

Creating a StandardUser

In order to create a StandardUser we can use similar code.

Single Table Inheritance

I stored this user in a variable and as you can see here, a StandardUser has access to the slug_generator method.

Single Table Inheritance

But the AdminUser doesn’t have access to the same method because it was declared in the StandardUser subclass.

Single Table Inheritance

Therefore it gives a NoMethodError, as it should.

Single Table Inheritance Summary

I hope that you now understand how single table inheritance can be used in application development. In the show notes I’ve included a link to a tutorial where I walk through a step by step guide on how STI can be applied in a real world application, so please feel free to reference it to further your understanding, and good luck with the coding interview!

Show Notes

LEAVE A REPLY

Please enter your comment!
Please enter your name here