Skip to content

Commit ef7bc6d

Browse files
authored
MONGOID-3942 Test: Eager loading no longer broken when iterating criteria a second time
1 parent c493444 commit ef7bc6d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

spec/mongoid/criteria/includable_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,5 +1461,32 @@ class C
14611461
end
14621462
end
14631463
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
14641491
end
14651492
end

spec/mongoid/criteria/includable_spec_models.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ class IncBlogPost
4141
class IncAuthor
4242
include Mongoid::Document
4343
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

0 commit comments

Comments
 (0)