mongoid/mongoid — pull request #2454 by jamesalmond (2012-10-09 15:25:02+00:00)
Title: Removing the last embedded document doesn't remove it from as_document

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](https://github.com/mongoid/mongoid/blob/master/lib/mongoid/document.rb#L190). 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?