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

Criteria::Modifiable#create_document ignores Hashes

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0, 5.1.2
    • Affects Version/s: None
    • Component/s: None
    • None

      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.

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

              Created:
              Updated:
              Resolved: