Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1735

Model.find provides inconsistent behaviour

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.0.0
    • Component/s: None

      Description

      Hi,

      We've come across an inconsistent behaviour with Mongoid regarding the use of Model.find.

      You have a model (Hangout), and 2 Object Ids for documents contained in the model's collection. Say you have a scope which filters those documents based on an attribute. When applying that scope (upcoming), the first document should not be returned, but the second should.

      > hangout_ids
      => [BSON::ObjectId('4f42466d96ee61c0f10000bd'), BSON::ObjectId('4f42467596ee61c0f10000d2')]
      > Hangout.upcoming.find(hangout_ids)
      => [#<Hangout _id: 4f42467596ee61c0f10000d2>]

      Now, let's try and pass the id of the first document into that model's scoped find method:

      > Hangout.upcoming.find(hangout_ids.first)
      => Mongoid::Errors::DocumentNotFound: Document not found for class Hangout with id(s) 4f42466d96ee61c0f10000bd.

      If we try the same query above but with the last item in the hangout_ids array, it returns the array with the document.

      What seems to be happening instead is that if a single document is found among an array of ids, then the DocumentNotFound exception will not be raised at all. We are therefore dependent on an expectation that at least one of the ids in an array matches a document in the collection. This seems like an inconsistent behaviour.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: