Fix `Relation#transaction` to not apply a default scope

Fix: https://github.com/rails/rails/issues/50368

When called on a scope it would go through the default scoping
delegator that applies a default scope, which make no sense.
This commit is contained in:
Jean Boussier 2023-12-19 11:28:37 +01:00
parent 462e8e8b1c
commit 8c9ff7cdd8
2 changed files with 9 additions and 1 deletions

View File

@ -101,7 +101,7 @@ module ActiveRecord
:to_sentence, :to_fs, :to_formatted_s, :as_json,
:shuffle, :split, :slice, :index, :rindex, to: :records
delegate :primary_key, :connection, to: :klass
delegate :primary_key, :connection, :transaction, to: :klass
module ClassSpecificRelation # :nodoc:
extend ActiveSupport::Concern

View File

@ -36,6 +36,14 @@ class TransactionTest < ActiveRecord::TestCase
assert_equal title_change, topic.changes["title"]
end
def test_transaction_does_not_apply_default_scope
# Regression test for https://github.com/rails/rails/issues/50368
topic = topics(:fifth)
Topic.where.not(id: topic.id).transaction do
assert_not_nil Topic.find(topic.id)
end
end
if !in_memory_db?
def test_rollback_dirty_changes_even_with_raise_during_rollback_removes_from_pool
topic = topics(:fifth)