Single Table Inheritance with Sequel

I struggled with STI with Sequel as there is very little documentation on using plugins, let alone this specific one. It turns out it is incredibly trivial, but did require trawling through the code base to figure it out.

In your super class you need to activate the plugin (Sequel::Plugins::SingleTableInheritance), and specify the column name to be used to hold the name of the specific class for each object. In this case the super class is called Fruit, and the field I am using is object_type

class Fruit < Sequel::Model
   plugin :single_table_inheritance, :object_type

It’s really important that the field name is a symbol. If it is a string it will go bang.

Obviously you need to create the field object_type in your database table fruits as some kind of text field long enough to hold the name of the superclass and any subclass.

Your subclasses are trivial, simply extend your superclass.

class Apple < Fruit


Hope this helps someone!

Leave a Comment