File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -1461,5 +1461,32 @@ class C
1461
1461
end
1462
1462
end
1463
1463
end
1464
+
1465
+ context "when including an association and using each twice on a criteria" do
1466
+
1467
+ let ( :criteria ) { IncPost . all . includes ( :person ) }
1468
+
1469
+ before do
1470
+ p = IncPerson . create! ( name : "name" )
1471
+ 4 . times { IncPost . create! ( person : p ) }
1472
+ criteria
1473
+ expect_query ( 2 ) do
1474
+ criteria . each ( &:person )
1475
+ end
1476
+ end
1477
+
1478
+ # The reason we are checking for two operations here is:
1479
+ # - The first operation gets all of the posts
1480
+ # - The second operation gets the person from the first post
1481
+ # Now, all subsequent posts should use the eager loaded person when
1482
+ # trying to retrieve their person.
1483
+ # MONGOID-3942 reported that after iterating the criteria a second time,
1484
+ # the posts would not get the eager loaded person.
1485
+ it "eager loads the criteria" do
1486
+ expect_query ( 2 ) do
1487
+ criteria . each ( &:person )
1488
+ end
1489
+ end
1490
+ end
1464
1491
end
1465
1492
end
Original file line number Diff line number Diff line change @@ -41,3 +41,14 @@ class IncBlogPost
41
41
class IncAuthor
42
42
include Mongoid ::Document
43
43
end
44
+
45
+ class IncPost
46
+ include Mongoid ::Document
47
+ belongs_to :person , class_name : "IncPerson"
48
+ end
49
+
50
+ class IncPerson
51
+ include Mongoid ::Document
52
+ has_many :posts , class_name : "IncPost"
53
+ field :name
54
+ end
You can’t perform that action at this time.
0 commit comments