Skip to content

Commit 6407218

Browse files
authored
MONGOID-5164 Use None criteria instead of $in with empty list in HABTM association (#5467)
* MONGOID-5164 see if test fails when executing a query * MONGOID-5164 fix tests * omit apply_scope
1 parent bfdaa99 commit 6407218

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/mongoid/association/referenced/has_and_belongs_to_many.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,12 @@ def with_ordering(criteria)
263263

264264
def query_criteria(id_list)
265265
crit = relation_class.criteria
266-
crit = crit.apply_scope(scope)
267-
crit = crit.all_of(primary_key => {"$in" => id_list || []})
266+
crit = if id_list
267+
crit = crit.apply_scope(scope)
268+
crit.all_of(primary_key => { "$in" => id_list })
269+
else
270+
crit.none
271+
end
268272
with_ordering(crit)
269273
end
270274
end

spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
end
112112

113113
let(:criteria) do
114-
Preference.all_of("_id" => { "$in" => [] })
114+
Preference.none
115115
end
116116

117117
it "a criteria object" do

0 commit comments

Comments
 (0)