Defer loading of ActiveRecord to avoid config issues #852
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If FriendlyId references
ActiveRecord::Base
while it is loading as part of a Rails application, it triggers theon_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 innew_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 toActiveRecord::Base
in an initializer block. The simplest way of doing this is with aRailtie
, 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