Devchat.tv Episode Roundup

003 RC Single Table Inheritance (STI)


Listen Later

Single Table Inheritance is a slightly advanced topic in Ruby on Rails. It stems from class inheritance, which is a core Object Oriented Programming principle.
In Ruby, if you inherit one class from another, you use the '<' operator. Here's an example:
class Truck < Car
end
In Rails, this gets tricky because when inheriting a Model, do you use the parent models connection and table? or your own? By convention, you use the parent's table, which means that you need some way of differentiating the Trucks from the rest of the Cars.
So, by adding a 'type' column to your table—which is a string—you activate a Rails convention that stores the subclass' classname in the type column. So, in the case of the Truck class, you'd have all of the Car's attributes and a type column that is populated with the string "Truck" to denote that it's a truck.
When doing a Model lookup on your database, Rails tacks on the "AND type = 'Truck'" for you.
Download this Episode
...more
View all episodesView all episodes
Download on the App Store

Devchat.tv Episode RoundupBy Devchat.tv