From 17f2f3054c218485fff598966c39cd3d8a6f6056 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 5 Apr 2019 12:51:13 +0900 Subject: Association loading isn't to be affected by scoping consistently Follow-up of 5c71000, #29834, and #30271. Currently, preloading and eager loading are not to be affected by scoping, with the exception of `unscoped`. But non eager loaded association access is still affected by scoping. Although this is a breaking change, the association loading will work consistently whether preloaded / eager loaded or not. Before: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => nil Comment.preload(:post).find(1).post # => # Comment.eager_load(:post).find(1).post # => # end ``` After: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => # Comment.preload(:post).find(1).post # => # Comment.eager_load(:post).find(1).post # => # end ``` Fixes #34638. Fixes #35398. --- activerecord/test/cases/associations/eager_test.rb | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'activerecord/test/cases/associations/eager_test.rb') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index cd9c8a5285..594d161fa3 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1400,11 +1400,24 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal expected, FirstPost.unscoped.find(2) end - test "preload ignores the scoping" do - assert_equal( - Comment.find(1).post, - Post.where("1 = 0").scoping { Comment.preload(:post).find(1).post } - ) + test "belongs_to association ignores the scoping" do + post = Comment.find(1).post + + Post.where("1=0").scoping do + assert_equal post, Comment.find(1).post + assert_equal post, Comment.preload(:post).find(1).post + assert_equal post, Comment.eager_load(:post).find(1).post + end + end + + test "has_many association ignores the scoping" do + comments = Post.find(1).comments.to_a + + Comment.where("1=0").scoping do + assert_equal comments, Post.find(1).comments + assert_equal comments, Post.preload(:comments).find(1).comments + assert_equal comments, Post.eager_load(:comments).find(1).comments + end end test "deep preload" do -- cgit v1.2.3