module Kithe::StiPreload

From: guides.rubyonrails.org/v6.0/autoloading_and_reloading_constants.html#single-table-inheritance

https://edgeguides.rubyonrails.org/autoloading_and_reloading_constants.html#single-table-inheritance

While this is recommended starting with Rails6 and zeitwerk, it will work fine under Rails 5.2 and previous also, to make sure all sub-classes in db are loaded, so ActiveRecord knows how to create SQL WHERE clauses on particular inheritance hieararchies.

We include in our Kithe::Model, which uses Single-Table Inheritance

Public Instance Methods

descendants() click to toggle source
Calls superclass method
# File lib/kithe/sti_preload.rb, line 20
def descendants
  preload_sti unless preloaded
  super
end
preload_sti() click to toggle source

Constantizes all types present in the database. There might be more on disk, but that does not matter in practice as far as the STI API is concerned.

Assumes store_full_sti_class is true, the default.

# File lib/kithe/sti_preload.rb, line 30
def preload_sti
  types_in_db = \
    base_class.
      unscoped.
      select(inheritance_column).
      distinct.
      pluck(inheritance_column).
      compact

  types_in_db.each do |type|
    logger.debug("Preloading Single-Table Inheritance type #{type} for #{base_class.name}")
    type.constantize
  end

  self.preloaded = true
end