Fix broken background image after removal (#2817)

ActiveStorage::Record and ApplicationRecord used separate connection
pools (from separate `connects_to` calls). This meant `belongs_to
touch: true` on attachments silently failed — the card couldn't join the
attachment's transaction to receive the deferred touch, so fragment
caches were never invalidated after image removal.

Fix by delegating `connection_pool` from ActiveStorage::Record to
ApplicationRecord so they share a single pool.
This commit is contained in:
Mike Dalessio
2026-04-09 12:33:27 -04:00
committed by GitHub
parent 759e6079e8
commit b07f592aaf
2 changed files with 34 additions and 8 deletions
+8 -8
View File
@@ -19,15 +19,15 @@ ActiveSupport.on_load(:action_text_content) do
end
end
# Don't configure replica connections for ActiveStorage::Record.
# When ActiveStorage uses `connects_to`, 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, causing FileNotFoundError when
# using `process: :immediately` for variants.
# See: https://github.com/rails/rails/issues/53694
# ApplicationRecord calls `configure_replica_connections` to set up connection pools for the
# application models. We want ActiveStorage::Record to use the same pools for transactional
# integrity, proper callback invocation, joins, etc., however ActiveStorage::Record inherits from
# ActiveRecord::Base, not ApplicationRecord. This is how we make Active Storage always use the
# ApplicationRecord connection pool.
ActiveSupport.on_load(:active_storage_record) do
configure_replica_connections
class << self
delegate :connection_pool, to: "ApplicationRecord"
end
end
module ActiveStorageControllerExtensions