Fix ActiveStorage FileNotFoundError with immediate variant processing (#2022)

When ActiveStorage::Record uses `connects_to` for read replica support,
it creates a separate connection pool from ApplicationRecord. This causes
`after_commit` callbacks to fire in non-deterministic order - the
Attachment's `create_variants` callback can fire before the User model's
upload callback completes, resulting in FileNotFoundError.

The fix removes replica connection configuration from ActiveStorage::Record
so it shares the same connection pool as application models, ensuring
proper callback ordering.

Also reverts test workarounds that were added to work around this issue,
since the root cause is now fixed.

See: https://github.com/rails/rails/issues/53694
This commit is contained in:
Jeremy Daer
2025-12-08 14:44:49 -08:00
committed by GitHub
parent cb0e9b9962
commit 49c4f2adc6
5 changed files with 19 additions and 31 deletions
@@ -22,12 +22,11 @@ class Notification::BundleMailerTest < ActionMailer::TestCase
end
test "renders avatar with external image URL when avatar is attached" do
blob = ActiveStorage::Blob.create_and_upload!(
@user.avatar.attach(
io: File.open(Rails.root.join("test", "fixtures", "files", "avatar.png")),
filename: "avatar.png",
content_type: "image/png"
)
@user.avatar.attach(blob)
create_notification(@user)