Rails Coach

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

Rails CoachBy Charles Max Wood

  • 4.7
  • 4.7
  • 4.7
  • 4.7
  • 4.7

4.7

3 ratings


More shows like Rails Coach

View all
Teach Me To Code » Screencasts by Charles Max Wood

Teach Me To Code » Screencasts

11 Listeners

JavaScript Jabber by Charles M Wood

JavaScript Jabber

234 Listeners

iPhreaks by Charles M Wood

iPhreaks

17 Listeners

Ruby Rogues by Charles M Wood

Ruby Rogues

45 Listeners

The Freelancers' Show by Charles M Wood

The Freelancers' Show

23 Listeners

React Native Radio by Jamon Holmgren, Robin Heinze, Mazen Chami

React Native Radio

59 Listeners

My JavaScript Story by Charles M Wood

My JavaScript Story

4 Listeners

JavaScript Jabber by Charles M Wood

JavaScript Jabber

62 Listeners

Ruby Rogues by Charles M Wood

Ruby Rogues

21 Listeners

Adventures in Angular by Charles M Wood

Adventures in Angular

15 Listeners