"where"query converting values to BSON::ObjectId incorrectly

XMLWordPrintableJSON

    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Given these models:

      class Entity
        include Mongoid::Document
        field :entity_ref_ids, :type => Array
      end
      
      class EntityRef
        include Mongoid::Document
        identity :type => String # using Strings for _id, not BSON::ObjectId
      end
      

      A query like this:

      Entity.where(:entity_ref_ids => "4e2e900a0bed3fb1a0000005")
      

      results in "4e2e900a0bed3fb1a0000005" being converted to a BSON::ObjectId, which is wrong.

      I can see the problem is in Mongoid::Extensions::ObjectId::Conversions#convert, the line

      next unless key.to_s =~ /id/
      

      It does check using_object_ids?, but only on the Entity model. So what the code is saying, is "I will attempt to convert any field containing 'id' (_id or otherwise) to BSON::ObjectId, unless my class is not using BSON::ObjectId for my primary key". It doesn't seem right to me.

      One possible fix could be to make that line be == '_id', rather than match any field name containing "id", but then it would break for people /expecting/ the type conversion. I don't really know what's best, without getting into the code a bit more. I will do that at some point, but any suggestions now?

      Cheers,

      Ben

              Assignee:
              Unassigned
              Reporter:
              Ben Symonds
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved: