Skip to content

Commit 998acae

Browse files
committed
MONGOID-4405 Check if relation is cyclic as well as embedded before querying
1 parent 2376d57 commit 998acae

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/mongoid/scopable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def default_scopable?
117117
# @since 3.0.0
118118
def queryable
119119
crit = Threaded.current_scope(self) || Criteria.new(self)
120-
crit.embedded = true if crit.klass.embedded?
120+
crit.embedded = true if (crit.klass.embedded? && !crit.klass.cyclic?)
121121
crit
122122
end
123123

spec/mongoid/relations/cyclic_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
it "sets cyclic to true" do
2222
expect(document.cyclic).to be true
2323
end
24+
25+
context "when a query is executed" do
26+
27+
before do
28+
document.save
29+
end
30+
31+
it "queries the database, not memory" do
32+
expect(Role.first).to be_a(Role)
33+
end
34+
end
2435
end
2536

2637
context "when the name is not inflected easily" do
@@ -133,6 +144,17 @@ def is_root?
133144
expect(document.class.relations['child_mango']).to be_cascading_callbacks
134145
end
135146
end
147+
148+
context "when a query is executed" do
149+
150+
before do
151+
document.save
152+
end
153+
154+
it "queries the database, not memory" do
155+
expect(Shelf.first).to be_a(Shelf)
156+
end
157+
end
136158
end
137159

138160
context "when building a namespaced hierarchy" do

0 commit comments

Comments
 (0)