From 22360534ac922c68fb0a28f584b48bc0f3633221 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Thu, 7 Feb 2019 17:44:23 +0900 Subject: Fix `relation.create` to avoid leaking scope to initialization block and callbacks `relation.create` populates scope attributes to new record by `scoping`, it is necessary to assign the scope attributes to the record and to find STI subclass from the scope attributes. But the effect of `scoping` is class global, it was caused undesired behavior that pollute all class level querying methods in initialization block and callbacks (`after_initialize`, `before_validation`, `before_save`, etc), which are user provided code. To avoid the leaking scope issue, restore the original current scope before initialization block and callbacks are invoked. Fixes #9894. Fixes #17577. Closes #31526. --- activerecord/test/cases/base_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases/base_test.rb') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 4938b6865f..363593ca19 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1535,7 +1535,7 @@ class BasicsTest < ActiveRecord::TestCase Bird.create!(name: "Bluejay") ActiveRecord::Base.connection.while_preventing_writes do - assert_queries(2) { Bird.where(name: "Bluejay").explain } + assert_nothing_raised { Bird.where(name: "Bluejay").explain } end end -- cgit v1.2.3