-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
Such criteria incorrectly format query strings.
The following spec fails:
require 'spec_helper' describe Mongoid::Criterion::Inclusion do before do [ Person, Post, Product, Game, Jar ].each(&:delete_all) end describe "#any_in" do context "on foreign keys" do context "when not using object ids" do before(:all) do Person.identity :type => String end after(:all) do Person.identity :type => BSON::ObjectId end let!(:person) { Person.create } let!(:account) { person.create_account(:name => "test") } let(:from_db) { Account.any_in(:person_id => [ person.id ]).to_a } it "should issue proper queries" do from_db.should == [ account ] end end end end end
Test executes the following (incorrect) query:
MONGODB mongoid_76988['accounts'].find({:person_id=>"{\"$in\"=>[\"4ed5e37fe269cb2cbc000002\"]}"})
This is a regression from v2.3.3. It was introduced by fe1472cd along with a fix for MONGOID-1400. I tested it on any_in only, however, all criteria should fail under similar conditions.
The culprit is Mongoid::Fields::Serializable::ForeignKeys::Object#serialize:
def serialize(object) if object_id_field? constraint.convert(object) else metadata.klass.fields["_id"].serialize(object) end end
When the field is not an object_id_field, this code always escapes field's value, regardless of whether it is a proper value or an operator.