ruby 47 lines · 8 steps

How STI models share behavior in Rails

A single Employee table backs four classes that inherit validations and override pay logic through single-table inheritance.

Explained by highlit
1class Employee < ApplicationRecord
2 validates :name, presence: true
3 validates :salary, numericality: { greater_than: 0 }
4 
5 scope :managers, -> { where(type: "Manager") }
6 scope :engineers, -> { where(type: "Engineer") }
7 
8 def weekly_pay
9 (salary / 52.0).round(2)
10 end
11 
12 def role
13 type&.underscore&.humanize || "Employee"
14 end
15end
16 
17class Manager < Employee
18 has_many :reports, class_name: "Employee", foreign_key: :manager_id
19 
20 def weekly_pay
21 super + bonus_share
22 end
23 
24 def bonus_share
25 (annual_bonus.to_f / 52.0).round(2)
26 end
27end
28 
29class Engineer < Employee
30 validates :primary_language, presence: true
31 
32 def weekly_pay
33 super + on_call_stipend
34 end
35 
36 def on_call_stipend
37 on_call? ? 200.0 : 0.0
38 end
39end
40 
41class Contractor < Employee
42 validates :hourly_rate, numericality: { greater_than: 0 }
43 
44 def weekly_pay
45 (hourly_rate * hours_per_week).round(2)
46 end
47end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Single-table inheritance lets subclasses share one table while specializing behavior through Ruby's normal class hierarchy.
  2. 2Calling super in an overridden method composes subclass-specific additions on top of the base implementation instead of replacing it.
  3. 3Validations and scopes defined on the base model apply to every subclass, while subclasses can add their own constraints.

Related explainers

Share this explainer

Here's the card — post it anywhere.

How STI models share behavior in Rails — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code