Skip to content

Do not instantiate ActiveRecord model early#1708

Closed
tachyons wants to merge 1 commit intodoorkeeper-gem:mainfrom
tachyons:fix_active_record_loading
Closed

Do not instantiate ActiveRecord model early#1708
tachyons wants to merge 1 commit intodoorkeeper-gem:mainfrom
tachyons:fix_active_record_loading

Conversation

@tachyons
Copy link
Copy Markdown

Summary

Do not instantiate active record models before loading active record, as it can cause several side effects.

Fixes #1703

Do not instantiate active record models before loading active record, as
it can cause several side effects.

Fixes doorkeeper-gem#1703
@nbulaj
Copy link
Copy Markdown
Member

nbulaj commented May 15, 2024

I remember we already had such hook and it also lead to a lot of issues, unfortunately. I believe we can find them in the issues section

Copy link
Copy Markdown
Member

@nbulaj nbulaj left a comment

Choose a reason for hiding this comment

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

Well, let's try to use AS hooks once again 🤞

@nbulaj
Copy link
Copy Markdown
Member

nbulaj commented Jun 2, 2024

Can you please add a changelog entry?

@tachyons
Copy link
Copy Markdown
Author

tachyons commented Jun 3, 2024 via email

@nbulaj
Copy link
Copy Markdown
Member

nbulaj commented Dec 9, 2025

Hello @tachyons . maybe any updates on that? 👁️

55728 added a commit to 55728/doorkeeper that referenced this pull request Mar 25, 2026
Wrap `initialize_configured_associations` body in `ActiveSupport.on_load(:active_record)` to prevent Doorkeeper from instantiating ActiveRecord models too early during the Rails boot process.

When Doorkeeper calls `constantize` on model class names (e.g. `access_token_model`, `application_model`) inside `config.to_prepare`, it can trigger early loading of ActiveRecord before `config.active_record.*` settings have been applied. This causes framework defaults (set in `new_framework_defaults.rb` during Rails upgrades) to be silently ignored.

This change defers the model loading until ActiveRecord is fully initialized, which:

- Fixes `config.active_record.*` settings being ignored (Rails 7.0→7.1 upgrade scenario reported in doorkeeper-gem#1703)
- Aligns with the pattern recommended by Rails core (rails/rails#50133, rails/rails#46047)
- Preserves STI subclass support (verified with dedicated tests)

Fixes doorkeeper-gem#1703

Based on the approach from doorkeeper-gem#1708 by @tachyons, with the addition of CHANGELOG entry and STI-related tests as requested by @nbulaj.
55728 added a commit to 55728/doorkeeper that referenced this pull request Mar 26, 2026
Wrap `initialize_configured_associations` body in `ActiveSupport.on_load(:active_record)` to prevent Doorkeeper from instantiating ActiveRecord models too early during the Rails boot process.

When Doorkeeper calls `constantize` on model class names (e.g. `access_token_model`, `application_model`) inside `config.to_prepare`, it can trigger early loading of ActiveRecord before `config.active_record.*` settings have been applied. This causes framework defaults (set in `new_framework_defaults.rb` during Rails upgrades) to be silently ignored.

This change defers the model loading until ActiveRecord is fully initialized, which:

- Fixes `config.active_record.*` settings being ignored (Rails 7.0→7.1 upgrade scenario reported in doorkeeper-gem#1703)
- Aligns with the pattern recommended by Rails core (rails/rails#50133, rails/rails#46047)
- Preserves STI subclass support (verified with dedicated tests)

Fixes doorkeeper-gem#1703

Based on the approach from doorkeeper-gem#1708 by @tachyons, with the addition of CHANGELOG entry and STI-related tests as requested by @nbulaj.
@nbulaj
Copy link
Copy Markdown
Member

nbulaj commented Apr 16, 2026

We merged #1804 so I believe we can close this one. Thanks!

@nbulaj nbulaj closed this Apr 16, 2026
@tachyons
Copy link
Copy Markdown
Author

Thanks @nbulaj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Doorkeeper is loading ActiveRecord too early

3 participants