Skip to content
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

Defer loading of ActiveRecord to avoid config issues #852

Merged
merged 1 commit into from
Jan 15, 2018
Merged

Defer loading of ActiveRecord to avoid config issues #852

merged 1 commit into from
Jan 15, 2018

Conversation

lazyatom
Copy link
Contributor

If FriendlyId references ActiveRecord::Base while it is loading as part of a Rails application, it triggers the on_load event for ActiveRecord, which as part of the loading process sets the current configuration values of ActiveRecord. If any initializers try to influence that configuration (e.g. changing defaults in new_framework_defaults.rb for an app that's been upgraded), these changes will have no effect because the configuration has already been set in place.

To avoid this, we need to avoid triggering the on_load event by wrapping the reference to ActiveRecord::Base in an initializer block. The simplest way of doing this is with a Railtie, which will be automatically loaded if FriendlyId is required as part of Rails.

Testing this change is unfortunately quite hard; it would require setting up a Rails test harness application to exercise the Railtie. Since FriendlyId doesn't currently have that, I've tried to keep the
change small.

Fixes #823

If FriendlyId references `ActiveRecord::Base` while it is loading as
part of a Rails application, it triggers the `on_load` event for
ActiveRecord, which as part of the loading process sets the current
configuration values of ActiveRecord. If any initializers try to
influence that configuration (e.g. changing defaults in
`new_framework_defaults.rb` for an app that's been upgraded), these
changes will have no effect because the configuration has already been
set in place.

To avoid this, we need to avoid triggering the `on_load` event by
wrapping the reference to `ActiveRecord::Base` in an initializer
block. The simplest way of doing this is with a `Railtie`, which will
be automatically loaded if FriendlyId is required as part of Rails.

Fixes #823
@parndt parndt merged commit 4bd4300 into norman:master Jan 15, 2018
@parndt
Copy link
Collaborator

parndt commented Jan 15, 2018

Thank you!

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.

2 participants