Repository navigation
Include ArSync::ModelBase via on_load hook in generated initializer - #159
Merged
Merged
Conversation
The install generator wrote `ActiveRecord::Base.include ArSync::ModelBase`
as the first line of config/initializers/ar_sync.rb. Initializers are
loaded in filename order, and ar_sync.rb comes before
new_framework_defaults_*.rb, so this line loaded ActiveRecord::Base and
fired the `ActiveSupport.on_load(:active_record)` hooks before those
files ran. Rails copies several `config.active_record.*` values
(e.g. default_column_serializer,
run_commit_callbacks_on_first_saved_instances_in_transaction) into
ActiveRecord::Base inside that hook, so settings in later initializers
were silently ignored.
Generate `ActiveSupport.on_load(:active_record) { include ArSync::ModelBase }`
instead, as the Rails guide recommends for code that extends framework
classes. Apps that already ran the generator keep their existing
initializer; this only changes newly generated ones.
The gem's own lib/ does not touch ActiveRecord::Base at the top level,
so no change is needed there.
References:
- https://guides.rubyonrails.org/configuring.html#load-hooks
- rails/rails#31285
- rails/rails#33283
- rails/rails#56201
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xQkfP5otTax8TVwNE5nEg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The install generator wrote this as the first line of
config/initializers/ar_sync.rb:Initializers load in filename order, and
ar_sync.rbcomes beforenew_framework_defaults_*.rb. This line loadedActiveRecord::Baseand ran theActiveSupport.on_load(:active_record)hooks before those files. Rails copies severalconfig.active_record.*values (e.g.default_column_serializer,run_commit_callbacks_on_first_saved_instances_in_transaction) intoActiveRecord::Baseinside that hook, so settings in later initializers were silently ignored.This PR changes the generated line to:
Apps that already ran the generator keep their existing initializer; this only changes newly generated ones. The gem's own
lib/does not touchActiveRecord::Baseat the top level, so no change is needed there.Background
Related
ActiveRecord::Baseat require time. Without it, apps using ar_sync still load Base early via ar_serializer.🤖 Generated with Claude Code
https://claude.ai/code/session_019xQkfP5otTax8TVwNE5nEg