Rails Delegated Types

Aug 22, 2024 #rails #ruby

Today I stumbled upon Rails delegated types, a feature added in Rails 7. The paradigm can be used as an alternative to single table inheritance and purely abstract ActiveRecord classes. Keeping this one in my back pocket to use next time I come across a problem where I want the superclass view to be separately "queryable" in its own table without having to store all of unrelated subclass attributes in that same table.

With this approach, the “superclass” is a concrete class that is represented by its own table, where all the superclass attributes that are shared amongst all the “subclasses” are stored. And then each of the subclasses have their own individual tables for additional attributes that are particular to their implementation.