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

Removing the last embedded document doesn't remove it from as_document

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.10
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I am trying to duplicate/dup a document that has embedded documents that have been manipulated but not saved and have found an issue. Particularly when an embeds_many relation becomes empty. I've traced the dup method down to the as_document method in document.rb. When updating the attributes hash with the embedded document's attributes it doesn't update where the relation [is "blank".
      ](https://github.com/mongoid/mongoid/blob/master/lib/mongoid/document.rb#L195)

      So if I have ParentDocument with embeds_many :child_documents, if I remove all the child documents and call as_document before saving, the child relation evaluates to [] which is a "blank" value so the attributes hash remains how it was before the child documents were deleted. If you, for example, have 2 embedded documents and you delete one of them you get 1 embedded document in as_document, which is expected. It's just going from n to 0 that seems to be a problem.

      I've added a failing spec to demonstrate it and a fix that passes the spec and all the other tests. Although, I'm not 100% confident that the fix fully expresses the problem I'm trying to solve. Any thoughts on this?

            Assignee:
            durran Durran Jordan
            Reporter:
            jamesalmond James Almond
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: