Skip to content

Load STI models explicitly. #19266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged

Load STI models explicitly. #19266

merged 1 commit into from
Jun 23, 2025

Conversation

ba1ash
Copy link
Member

@ba1ash ba1ash commented Jun 20, 2025

Due to STI nature it is required. Otherwise undesired behavior is possible
in dev and test environments(where usually lazy loading is enabled).
The udesired behavior can be like:
Fetching not loaded yet STI model through its parent model
(e.g. User.find(service_account_id) raises ActiveRecord::NotFound, because
ServiceAccount has not been referenced yet.

SubclassRegistry has been removed, because:

  1. .register_subclasses and .registered_subclasses produce unexpected results.

      # e.g.: Principal -> User -> ServiceAccount
      Principal.register_subclass(User)
    
      # Then
      Principal.registered_subclasses == [User] # true
      User.registered_subclasses == [User] # true
      ServiceAccount.registered_subclasses == [User] # true
      # Having User as a subclass of User and ServiceAccount seems to be weird.
  2. There seems to be no big win in have the additional list of subclasses that have to be manually filled.

  3. Used in this PR approach seems to be simpler.
    It is just calling STI classes explicitly in to_prepare block of configuration.

Due to STI nature it is required. Otherwise undesired behavior is possible
in dev and test environments(where usually lazy loading is enabled).
The udesired behavior can be like:
  Fetching not loaded yet STI model through its parent model
  (e.g. `User.find(service_account_id)` raises `ActiveRecord::NotFound`, because
  `ServiceAccount` has not been referenced yet.

`SubclassRegistry` has been removed, because:
1. `.register_subclasses` and `.registered_subclasses` produce unexpected results.
    ```ruby
      # e.g.: Principal -> User -> ServiceAccount
      Principal.register_subclass(User)

      # Then
      Principal.registered_subclasses == [User] # true
      User.registered_subclasses == [User] # true
      ServiceAccount.registered_subclasses == [User] # true
      # Having User as a subclass of User and ServiceAccount seems to be weird.
    ```

2. There seems to be no big win in have the additional list of subclasses that have to be manually filled.
3. Used in commit apprach seems to be simpler.
   It is just calling STI classes explicitly in to_prepare block of configuration.
@ba1ash ba1ash self-assigned this Jun 20, 2025
@ba1ash ba1ash marked this pull request as ready for review June 20, 2025 15:34
Copy link
Contributor

@NobodysNightmare NobodysNightmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This is safer than the approach I suggested over in another PR, because it naturally uses the Autoloader to achieve the desired result. Also happy to see that the one class I added recently wasn't the only one forgotten so far, good job on finding all of them, as far as I can tell you were actively searching for other STI classes :)

@ba1ash ba1ash merged commit bf68334 into dev Jun 23, 2025
14 of 17 checks passed
@ba1ash ba1ash deleted the chore/sti-eager-loading branch June 23, 2025 11:16
@ba1ash ba1ash restored the chore/sti-eager-loading branch June 23, 2025 11:16
@ba1ash ba1ash deleted the chore/sti-eager-loading branch June 23, 2025 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants