{"issue":{"created_at":"2011-11-30T08:19:10Z","state":"closed","updated_at":"2011-12-01T16:43:25Z","comments":3,"user":{"gravatar_id":"9a184ada3f9b89bf43f8da95afe87aea","id":32590,"url":"https://api.github.com/users/grig","avatar_url":"https://secure.gravatar.com/avatar/9a184ada3f9b89bf43f8da95afe87aea?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"grig"},"body":"Such criteria incorrectly format query strings.\r\n\r\nThe following spec fails:\r\n\r\n```ruby\r\nrequire 'spec_helper'\r\n\r\ndescribe Mongoid::Criterion::Inclusion do\r\n  before do\r\n    [ Person, Post, Product, Game, Jar ].each(&:delete_all)\r\n  end\r\n\r\n  describe \"#any_in\" do\r\n    context \"on foreign keys\" do\r\n      context \"when not using object ids\" do\r\n        before(:all) do\r\n          Person.identity :type => String\r\n        end\r\n\r\n        after(:all) do\r\n          Person.identity :type => BSON::ObjectId\r\n        end\r\n\r\n        let!(:person) { Person.create }\r\n        let!(:account) { person.create_account(:name => \"test\") }\r\n        let(:from_db) { Account.any_in(:person_id => [ person.id ]).to_a }\r\n\r\n        it \"should issue proper queries\" do\r\n          from_db.should == [ account ]\r\n        end\r\n      end \r\n    end\r\n    end\r\nend\r\n```\r\n\r\nTest executes the following (incorrect) query:\r\n\r\n```\r\nMONGODB mongoid_76988['accounts'].find({:person_id=>\"{\\\"$in\\\"=>[\\\"4ed5e37fe269cb2cbc000002\\\"]}\"})\r\n```\r\n\r\nThis is a regression from v2.3.3. It was introduced by fe1472cd along with a fix for #1399. I tested it on `any_in` only, however, all criteria should fail under similar conditions.\r\n\r\nThe culprit is [Mongoid::Fields::Serializable::ForeignKeys::Object#serialize](https://github.com/mongoid/mongoid/blob/fe1472cd9d38cf99a3a68d0cfe64e36898267fc4/lib/mongoid/fields/serializable/foreign_keys/object.rb#L35):\r\n\r\n```ruby\r\ndef serialize(object)\r\n  if object_id_field?\r\n    constraint.convert(object)\r\n  else\r\n    metadata.klass.fields[\"_id\"].serialize(object)\r\n  end\r\nend\r\n```\r\n\r\nWhen 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.","assignee":{"gravatar_id":"6f09262a13427532461b0b618d445b1e","id":9030,"url":"https://api.github.com/users/durran","avatar_url":"https://secure.gravatar.com/avatar/6f09262a13427532461b0b618d445b1e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"durran"},"id":2398575,"labels":[],"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":1461,"title":"Criteria on foreign keys fail when referenced models don't use ObjectIds ","html_url":"https://github.com/mongoid/mongoid/issues/1461","closed_at":"2011-12-01T11:23:31Z","url":"https://api.github.com/repos/mongoid/mongoid/issues/1461","milestone":{"created_at":"2011-10-06T16:38:59Z","state":"open","closed_issues":8,"due_on":"2011-12-12T08:00:00Z","creator":{"gravatar_id":"6f09262a13427532461b0b618d445b1e","id":9030,"url":"https://api.github.com/users/durran","avatar_url":"https://secure.gravatar.com/avatar/6f09262a13427532461b0b618d445b1e?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"durran"},"number":27,"description":"","title":"2.4.0","open_issues":24,"url":"https://api.github.com/repos/mongoid/mongoid/milestones/27"}},"action":"created","comment":{"created_at":"2011-12-01T16:43:24Z","updated_at":"2011-12-01T16:43:24Z","user":{"gravatar_id":"c8d1ee584ac4594b6196e51d6fc32d87","id":96628,"url":"https://api.github.com/users/rgabo","avatar_url":"https://secure.gravatar.com/avatar/c8d1ee584ac4594b6196e51d6fc32d87?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"rgabo"},"body":"@durran, we're running into the same issue. Symbol h4s are fully escaped as JS strings when the field is a foreign key (belongs_to, not using ObjectIds). Our example:\r\n\r\n```ruby\r\n[11] pry(main)> Foo.where(:bar_id.exists => true)\r\n=> #<Mongoid::Criteria\r\n  selector: {:bar_id=>\"{\\\"$exists\\\"=>true}\"},\r\n  options:  {:sort=>[[:_id, :asc]]},\r\n  class:    Foo,\r\n  embedded: false>\r\n```\r\n\r\nthe expected criteria would be (query where field is not a foreign key):\r\n\r\n```ruby\r\n[14] pry(main)> Foo.where(:baz_id.exists => true)\r\n=> #<Mongoid::Criteria\r\n  selector: {:baz_id=>{\"$exists\"=>true}},\r\n  options:  {:sort=>[[:_id, :asc]]},\r\n  class:    Foo,\r\n  embedded: false>\r\n```\r\n\r\nEverything works as expected when using `Foo.collection.find(...)`.","id":2976911,"url":"https://api.github.com/repos/mongoid/mongoid/issues/comments/2976911"}}