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

Criteria::Modifiable#create_document ignores Hashes

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.0.0, 5.1.2
    • Component/s: None
    • Labels:
      None
    • Sprint:
      Ruby Sprint 32, Ruby Sprint 33, Ruby Sprint 35

      Description

      I would expect this:

      class Doc
        include Mongoid::Document
        field :some_hash, type: Hash
      end
       
      doc = Doc.where(some_hash: { foo: :bar }).first_or_create
      doc.some_hash
      

      to be {{

      { foo: :bar }

      }} but in actuality it is nil.

      As it turns out, Criteria::Modifiable#create_document ignores Hashes completely. I believe this "feature" originated from the need to ignore complex conditions such as gt as evidenced by the comments from way back in 2010.

      In my opinion, it is a legitimate use case to e.g. first_or_create, build, … on criteria including Hashes but I'm not sure how much effort would be required to support that use case without introducing unintended side-effects. I am creating this issue to solicit comments and/or get a definitive WONTFIX decision. In the latter case, I think the documentation should be updated to point out the aforementioned restriction.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              emily.stolfo Emily Stolfo
              Reporter:
              niels Niels Ganser
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: