Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tropical on Rails 26' | Modeling Class Hierarch...

Tropical on Rails 26' | Modeling Class Hierarchies in Active Record

Single-table inheritance and purely abstract classes work… until they don’t.

So where do you turn when things start to get messy?

In this talk, we break down single-table inheritance, purely abstract classes, and delegated types, discussing when each makes sense, where they fall short, and how to choose the right tool for the job.

Avatar for Nicolas Erlichman

Nicolas Erlichman

April 14, 2026

Other Decks in Programming

Transcript

  1. Tropical on Rails Why I really like modeling & polymorphism

    It can get messy Active Record offered only two ways to do inheritance
  2. Tropical on Rails Why I really like modeling & polymorphism

    It can get messy Active Record offered only two ways to do inheritance Delegated Types is still not widely known/used
  3. It is a pattern in Rails that allows multiple models

    to be stored in a single database table. Ruby on Rails Guides SINGLETABLE INHERITANCE
  4. Tropical on Rails Single-Table Inheritance Vehicle make, null: false model,

    null: false body_style, null: false Car bed_type, null: false Truck Car make model body_style Truck make model bed_type
  5. Tropical on Rails Single-Table Inheritance Vehicle make, null: false model,

    null: false type, null: ? body_style bed_type Vehicle make, null: false model, null: false body_style, null: false Car bed_type, null: false Truck Car make model body_style Truck make model bed_type Car Truck
  6. Class-table inheritance, where the superclass doesn't persist any attributes and

    all concrete subclasses must persist all the shared attributes themselves in their own tables. Rails Source Code PURELY ABSTRACT CLASSES
  7. Tropical on Rails Example Vehicle make, null: false model, null:

    false body_style, null: false Car bed_type, null: false Truck Car make model body_style Truck make model bed_type
  8. Tropical on Rails Example with Purely Abstract Classes Vehicle make,

    null: false model, null: false body_style, null: false Car bed_type, null: false Truck Car make model body_style Truck make model bed_type make, null: false model – null: false body_style, null: false Car Truck make – null: false model – null: false bed_type, null: false Vehicle
  9. Solves the STI problem of table bloat. This approach allows

    us to store shared attributes in a superclass table and have separate tables for subclass-specific attributes. Rails Source Code DELEGATED TYPES RAILS 6.1
  10. Tropical on Rails Delegated Types Vehicle make, null: false model,

    null: false body_style, null: false Car bed_type, null: false Truck Car make model body_style Truck make model bed_type
  11. Tropical on Rails Delegated Types body_style – null: false Car

    bed_type – null: false Truck Vehicle make – null: false model – null: false … Vehicle make, null: false model, null: false body_style, null: false Car bed_type, null: false Truck Car make model body_style Truck make model bed_type
  12. Tropical on Rails Benefits Unified queries Clean schemas (no table

    bloat) Scales naturally as new types appear
  13. If most attributes are shared, the set of subclasses is

    small and stable, and you often query across all types.
  14. If most attributes are shared, the set of subclasses is

    small and stable, and you often query across all types. Use Single-Table Inheritance
  15. If most attributes are shared, the set of subclasses is

    small and stable, and you often query across all types. Use Single-Table Inheritance If attributes differ significantly across types, and queries mostly happen within one concrete class.
  16. If most attributes are shared, the set of subclasses is

    small and stable, and you often query across all types. Use Single-Table Inheritance If attributes differ significantly across types, and queries mostly happen within one concrete class. Use Purely Abstract Classes
  17. If most attributes are shared, the set of subclasses is

    small and stable, and you often query across all types. Use Single-Table Inheritance If attributes differ significantly across types, and queries mostly happen within one concrete class. Use Purely Abstract Classes If attributes and validations differ, and you often need to query across all types.
  18. If most attributes are shared, the set of subclasses is

    small and stable, and you often query across all types. Use Single-Table Inheritance If attributes differ significantly across types, and queries mostly happen within one concrete class. Use Purely Abstract Classes If attributes and validations differ, and you often need to query across all types. Use Delegated Types