Skip to content

Include ArSync::ModelBase via on_load hook in generated initializer - #159

Merged
tompng merged 1 commit into
tompng:masterfrom
pocke:lazy_load_active_record_base
Oct 9, 2026
Merged

tompng merged 1 commit into
tompng:masterfrom
pocke:lazy_load_active_record_base

Conversation

@pocke

@pocke pocke commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Summary

The install generator wrote this as the first line of config/initializers/ar_sync.rb:

ActiveRecord::Base.include ArSync::ModelBase

Initializers load in filename order, and ar_sync.rb comes before new_framework_defaults_*.rb. This line loaded ActiveRecord::Base and ran the ActiveSupport.on_load(:active_record) hooks before those files. 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.

This PR changes the generated line to:

ActiveSupport.on_load(:active_record) { include ArSync::ModelBase }

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.

Background

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_019xQkfP5otTax8TVwNE5nEg

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

@tompng tompng left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

👍

@tompng
tompng merged commit eb76532 into tompng:master Oct 9, 2026
9 checks passed
@pocke
pocke deleted the lazy_load_active_record_base branch October 9, 2026 07:11
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