---
title: "Discussion on \"A Pattern for STIs in Rails\" | Hashnode"
description: "Discussion on \"A Pattern for STIs in Rails\". The Problem\nActive Record provides STIs to easily work with hierarchies of (persisted) models.\nThey have a few rough edges, however:\n\nActive Record needs to know the entire hierarchy to be able to build correct queries. This is at odds with lazy load..."
url: "https://hashnode.com/posts/a-pattern-for-stis-in-rails/635fe4da982ebb40a7eaa15d"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [A Pattern for STIs in Rails](https://fxn.hashnode.dev/a-pattern-for-stis-in-rails?utm_source=hashnode&utm_medium=feed)

The Problem Active Record provides STIs to easily work with hierarchies of (persisted) models. They have a few rough edges, however: Active Record needs to know the entire hierarchy to be able to build correct queries. This is at odds with lazy load...

Oct 31, 2022 · 3 min read · fxn.hashnode.dev  
Updated Feb 11, 2023  
Read the article

-   [#zeitwerk](https://hashnode.com/tag/zeitwerk)
-   [#sti](https://hashnode.com/tag/sti)
-   [#ruby-on-rails](https://hashnode.com/tag/ruby-on-rails)
-   [#rails](https://hashnode.com/tag/rails)

## Written by

[Xavier Noria](https://hashnode.com/@fxn)

@fxn · Barcelona

  
[View profile](https://hashnode.com/@fxn)

Everlasting student · Zeitwerk · Rails Core · Fukuoka Ruby Award · Ruby Hero · Freelance · Life lover

Everlasting student · Rails Core · Zeitwerk · Freelance · Life lover

## Responses 2

Markdown supported · ⌘↵ to post

[chaadow](https://hashnode.com/@chaadow)

[Nov 1, 2022](https://hashnode.com/posts/a-pattern-for-stis-in-rails/635fe4da982ebb40a7eaa15d/comment/636147467c8ae671518e0e9c)

> Could we go an extra mile and delay eager loading until some model in the hierarchy is loaded? Perhaps. Maybe Rails includes something like this in the future if we find an even better technique, we'll see.

Can't we use something like `Rails.autoloaders.main.on_load('MyModel') { ... }` ?

This can be useful when applying the strategy pattern, in the same vein as having a `_sti` as a suffi, a team can use a `xxxBase` suffix.

Which makes me want to ask you this question: Can we use a regular expression with the `on_load` hook?. This in combination with the `_sti` prefix idea, we can eager load a directory if it exists.

Basically the `Base` class will act as a router, look through the descendants and return the first applicable strategy class ( like ActiveJob::Serializers does )

Do you think this is feasible? Thanks for the article!

[Xavier Noria](https://hashnode.com/@fxn)

[Dec 10, 2022](https://hashnode.com/posts/a-pattern-for-stis-in-rails/635fe4da982ebb40a7eaa15d/comment/636147467c8ae671518e0e9c#reply-6394cdbc9fa713176ab9cb8f)

[chaadow](https://hashnode.com/@chaadow) Sorry, I got no notification for these comments!

That is a good idea and a version of that was documented. Problem is that it may lead to unsolvable circular dependencies that depend on load order. I am revamping the section in the guides about this topic.

[Dr Nic Williams](https://hashnode.com/@drnic)

[Nov 1, 2022](https://hashnode.com/posts/a-pattern-for-stis-in-rails/635fe4da982ebb40a7eaa15d/comment/636062d29d2c7c624f6999f8)

To confirm, whilst this idea won't auto discover new subclasses, it will auto-reload changes to any of the STI class files?

[Xavier Noria](https://hashnode.com/@fxn)

[Dec 10, 2022](https://hashnode.com/posts/a-pattern-for-stis-in-rails/635fe4da982ebb40a7eaa15d/comment/636062d29d2c7c624f6999f8#reply-6394cbe815750808b269b44e)

[Dr Nic Williams](https://hashnode.com/@drnic) Sorry, got no notification for your comment!

Yes, these classes will be reloaded when the application reloads, and the files watched too.
