File tree Expand file tree Collapse file tree 4 files changed +48
-8
lines changed
lib/active_record/associations Expand file tree Collapse file tree 4 files changed +48
-8
lines changed Original file line number Diff line number Diff line change
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
+
1
28
* Add ` rails db:prepare` to migrate or setup a database.
2
29
3
30
Runs ` db:migrate` if the database exists or ` db:setup` if it doesn' t.
Original file line number Diff line number Diff line change @@ -225,7 +225,7 @@ def association_scope
225
225
# Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the
226
226
# through association's scope)
227
227
def target_scope
228
- AssociationRelation . create ( klass , self ) . merge! ( klass . all )
228
+ AssociationRelation . create ( klass , self ) . merge! ( klass . scope_for_association )
229
229
end
230
230
231
231
def scope_for_create
Original file line number Diff line number Diff line change @@ -1400,11 +1400,24 @@ def test_preloading_has_many_through_with_custom_scope
1400
1400
assert_equal expected , FirstPost . unscoped . find ( 2 )
1401
1401
end
1402
1402
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
1408
1421
end
1409
1422
1410
1423
test "deep preload" do
Original file line number Diff line number Diff line change @@ -411,7 +411,7 @@ def test_forwarding_to_scoped
411
411
412
412
def test_nested_scope_finder
413
413
Comment . where ( "1=0" ) . scoping do
414
- assert_equal 0 , @welcome . comments . count
414
+ assert_equal 2 , @welcome . comments . count
415
415
assert_equal "a comment..." , @welcome . comments . what_are_you
416
416
end
417
417
@@ -452,7 +452,7 @@ def test_forwarding_of_static_methods
452
452
453
453
def test_nested_scope_finder
454
454
Category . where ( "1=0" ) . scoping do
455
- assert_equal 0 , @welcome . categories . count
455
+ assert_equal 2 , @welcome . categories . count
456
456
assert_equal "a category..." , @welcome . categories . what_are_you
457
457
end
458
458
You can’t perform that action at this time.
0 commit comments