From 45d3547ea06961afb563b6fcd8106c4406af8c54 Mon Sep 17 00:00:00 2001 From: Ashwin M Date: Tue, 27 Jan 2026 12:19:47 +0530 Subject: [PATCH] Skip Attributes patch for Rails 8.2+ Rails 8.2 (https://github.com/rails/rails/pull/54333) removed the `with_connection` call in `_default_attributes`, making the Attributes patch unnecessary for Rails 8.2+. This adds a version check to only apply the patch for Rails < 8.2.0.alpha. Co-Authored-By: Claude Opus 4.5 --- lib/active_record/tenanted/patches.rb | 5 +++-- lib/active_record/tenanted/railtie.rb | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/active_record/tenanted/patches.rb b/lib/active_record/tenanted/patches.rb index b5828e8..04f46d1 100644 --- a/lib/active_record/tenanted/patches.rb +++ b/lib/active_record/tenanted/patches.rb @@ -24,8 +24,9 @@ def with_temporary_pool(db_config, clobber: false) end end - # TODO: This monkey patch shouldn't be necessary after 8.1 lands and the need for a - # connection is removed. For details see https://github.com/rails/rails/pull/54348 + # This patch is only applied for Rails 8.1.x. Rails 8.2+ removed the with_connection + # call in _default_attributes (rails/rails#54333), making this patch unnecessary. + # See railtie.rb for the version check. module Attributes extend ActiveSupport::Concern diff --git a/lib/active_record/tenanted/railtie.rb b/lib/active_record/tenanted/railtie.rb index 423a351..61d4833 100644 --- a/lib/active_record/tenanted/railtie.rb +++ b/lib/active_record/tenanted/railtie.rb @@ -95,7 +95,11 @@ class Railtie < ::Rails::Railtie initializer "active_record_tenanted.monkey_patches" do ActiveSupport.on_load(:active_record) do - prepend ActiveRecord::Tenanted::Patches::Attributes + # Rails 8.2+ removed the with_connection call in _default_attributes (rails/rails#54333), + # so this patch is only needed for Rails 8.1.x + if ActiveRecord.version < Gem::Version.new("8.2.0.alpha") + prepend ActiveRecord::Tenanted::Patches::Attributes + end ActiveRecord::Tasks::DatabaseTasks.prepend ActiveRecord::Tenanted::Patches::DatabaseTasks end end