The only thing to do at the database level is to add a type column to the employees table that ActiveRecord will automatically use to identify the submodel. 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. In Rails, the type column is used to determine which type of model to instantiate for each row; a row with type ‘Article’ will make Rails call Article. Yes, but to enable STI, we have to add a type column to contain a string representing the type of the stored object.
Next we’ll look at two mechanisms for inheritance: single table inheritance (STI) and multiple table inheritance (MTI). In this case it would be assumed that a table called payments existed, and that it contained a type column. One of the most common of them is the usage of STI (Single Table Inheritance). This code is possible in Rails 4 where building association with STI type was fixed. Defines the column name for use with single table inheritance.
You should not use the type keyword as the column name because it is a reserved word for ActiveRecord. Rails single table inheritance: type coumn is not populated automatically. Sadly, this column doesn’t seem to be filled by Rails automatically:. Rails: Invalid single-table inheritance type error.
Chapter 10: Multiple Table Inheritance
Invalid single-table inheritance type: dog is not a subclass of Pet. Single Table Inheritance (STI) can help organize branching logic into separate classes, but a polymorphic association may be a better fit if there are unique database columns. Rails g migration add_profile_to_users profile:references profile_type. What is the purpose of the:type column for the User model in the STI example? Every index has to reference the type column, and you end up with indexes that are only relevant for a certain type. So is there a nice way to implement class table inheritance in Rails? In rails, if you name a column type, by default ActiveRecord allows inheritance by storing the name of the class in the column type. James writes about how the project originally used a STI approach to store the different content types and how they refactored to split each of the content types into their own table. From an ActiveRecord point of view, adding columns to a table for one of the content types will not increase the surface area of the API for the other content types. Rails will include a root key when rendering JSON.
Invalid Single-table Inheritance Type: Rails
The first thing you see is that for every row, a bunch of columns are empty. This is the major drawback of single table inheritance, and it prevents us to put a NOT NULL constraint on the columns that are not shared between models, even if these attributes are mandatory for the relevant model (and yes, you should definitely put NOT NULL constraints as well as FOREIGN KEY constraints in the database, but this is for another blog post). Single table inheritance is a way to emulate object-oriented inheritance in a relational database. In Ruby on Rails the field in the table called ‘type’ identifies the name of the class. In Hibernate (Java) and Entity Framework this pattern is called Table-Per-Class-Hierarchy and Table-Per-Hierarchy (TPH) respectively. Single Table Inheritance in Ruby on Rails means that you store the class name of the subclass in the type column in the database table.