Skip to content

Commit e9d1d76

Browse files
authored
Merge pull request rails#35868 from kamipo/association_isnt_to_be_affected_by_scoping_consistently
Association loading isn't to be affected by scoping consistently
2 parents 808c589 + 17f2f30 commit e9d1d76

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

activerecord/CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
* Association loading isn't to be affected by scoping consistently
2+
whether preloaded / eager loaded or not, with the exception of `unscoped`.
3+
4+
Before:
5+
6+
```ruby
7+
Post.where("1=0").scoping do
8+
Comment.find(1).post # => nil
9+
Comment.preload(:post).find(1).post # => #<Post id: 1, ...>
10+
Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
11+
end
12+
```
13+
14+
After:
15+
16+
```ruby
17+
Post.where("1=0").scoping do
18+
Comment.find(1).post # => #<Post id: 1, ...>
19+
Comment.preload(:post).find(1).post # => #<Post id: 1, ...>
20+
Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...>
21+
end
22+
```
23+
24+
Fixes #34638, #35398.
25+
26+
*Ryuta Kamizono*
27+
128
* Add `rails db:prepare` to migrate or setup a database.
229

330
Runs `db:migrate` if the database exists or `db:setup` if it doesn't.

activerecord/lib/active_record/associations/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def association_scope
225225
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
226226
# through association's scope)
227227
def target_scope
228-
AssociationRelation.create(klass, self).merge!(klass.all)
228+
AssociationRelation.create(klass, self).merge!(klass.scope_for_association)
229229
end
230230

231231
def scope_for_create

activerecord/test/cases/associations/eager_test.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,11 +1400,24 @@ def test_preloading_has_many_through_with_custom_scope
14001400
assert_equal expected, FirstPost.unscoped.find(2)
14011401
end
14021402

1403-
test "preload ignores the scoping" do
1404-
assert_equal(
1405-
Comment.find(1).post,
1406-
Post.where("1 = 0").scoping { Comment.preload(:post).find(1).post }
1407-
)
1403+
test "belongs_to association ignores the scoping" do
1404+
post = Comment.find(1).post
1405+
1406+
Post.where("1=0").scoping do
1407+
assert_equal post, Comment.find(1).post
1408+
assert_equal post, Comment.preload(:post).find(1).post
1409+
assert_equal post, Comment.eager_load(:post).find(1).post
1410+
end
1411+
end
1412+
1413+
test "has_many association ignores the scoping" do
1414+
comments = Post.find(1).comments.to_a
1415+
1416+
Comment.where("1=0").scoping do
1417+
assert_equal comments, Post.find(1).comments
1418+
assert_equal comments, Post.preload(:comments).find(1).comments
1419+
assert_equal comments, Post.eager_load(:comments).find(1).comments
1420+
end
14081421
end
14091422

14101423
test "deep preload" do

activerecord/test/cases/scoping/relation_scoping_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def test_forwarding_to_scoped
411411

412412
def test_nested_scope_finder
413413
Comment.where("1=0").scoping do
414-
assert_equal 0, @welcome.comments.count
414+
assert_equal 2, @welcome.comments.count
415415
assert_equal "a comment...", @welcome.comments.what_are_you
416416
end
417417

@@ -452,7 +452,7 @@ def test_forwarding_of_static_methods
452452

453453
def test_nested_scope_finder
454454
Category.where("1=0").scoping do
455-
assert_equal 0, @welcome.categories.count
455+
assert_equal 2, @welcome.categories.count
456456
assert_equal "a category...", @welcome.categories.what_are_you
457457
end
458458

0 commit comments

Comments
 (0)