-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 7.2.1
-
Component/s: Query
-
None
The and() criterion behaves incorrectly when Ruby symbols are passed instead of strings. The repro below demonstrates this issue. This issue is fixed by converting potential Ruby symbol usages to strings in the and() code.
Repro
For some DB, "Employees":
Band.create! Band.create! e_ids = Band.all.take(2).map(&:id); nil
=> [BSON::ObjectId('<oid_1>'), BSON::ObjectId('<oid_2>')]
Band.where(:id => { "$in" => e_ids }).selector
=> {"_id"=>{"$in"=>[BSON::ObjectId('<oid_1>')]}}
# specifying both as strings "$in" works -
Band.where(:id => { "$in" => e_ids }).and(:id => { "$in" => [ e_ids[1] ] }).selector => {"_id"=>{"$in"=>[BSON::ObjectId('<oid_1>')]}, "$and"=>[{"_id"=>{"$in"=>[BSON::ObjectId('<oid_1>')]}}]}
Employees.where(:id => { "$in" => e_ids }).and(:id => { "$in" => [ e_ids[1] ] }).count
=> 1
# specifying one as a symbol does not work -
Band.where(:id => { "$in" => e_ids }).and(:id => { :$in => [ e_ids[1] ] }).selector
NoMethodError: undefined method `start_with?' for :$in:Symbol
# specifying both as symbols also does not work
Band.where(:id => {:$in => e_ids }).and(:id => { :$in => [ e_ids[1] ] }).selector
NoMethodError: undefined method `start_with?' for :$in:Symbol
# but specifying one as a symbol without the and() does work
Band.where(:id => { :$in => e_ids }).selector
=> {"_id"=>{:$in=>[BSON::ObjectId('<oid_1>')]}}
Band.where(:id => { :$in => e_ids }).count
=> 2
- duplicates
-
MONGOID-5020 Condition lifting breaks with symbol operators on Ruby <= 2.6
- Closed