Skip to content

Commit c82fa92

Browse files
committed
Merge pull request #50395 from Shopify/fix-find-or-create
Fix `Relation#transaction` to not apply a default scope
1 parent 1f0b3ff commit c82fa92

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* Fix `Relation#transaction` to not apply a default scope
2+
3+
The method was incorrectly setting a default scope around its block:
4+
5+
```ruby
6+
Post.where(published: true).transaction do
7+
Post.count # SELECT COUNT(*) FROM posts WHERE published = FALSE;
8+
end
9+
```
10+
11+
*Jean Boussier*
12+
113
* Fix renaming primary key index when renaming a table with a UUID primary key
214
in PostgreSQL.
315

activerecord/lib/active_record/relation/delegation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def #{method}(...)
9090
:to_sentence, :to_fs, :to_formatted_s, :as_json,
9191
:shuffle, :split, :slice, :index, :rindex, to: :records
9292

93-
delegate :primary_key, :connection, to: :klass
93+
delegate :primary_key, :connection, :transaction, to: :klass
9494

9595
module ClassSpecificRelation # :nodoc:
9696
extend ActiveSupport::Concern

activerecord/test/cases/transactions_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ def test_rollback_dirty_changes
3030
assert_equal title_change, topic.changes["title"]
3131
end
3232

33+
def test_transaction_does_not_apply_default_scope
34+
# Regression test for https://github.com/rails/rails/issues/50368
35+
topic = topics(:fifth)
36+
Topic.where.not(id: topic.id).transaction do
37+
assert_not_nil Topic.find(topic.id)
38+
end
39+
end
40+
3341
if !in_memory_db?
3442
def test_rollback_dirty_changes_even_with_raise_during_rollback_removes_from_pool
3543
topic = topics(:fifth)

0 commit comments

Comments
 (0)