mongoid/mongoid — pull request #2561 by jonhyman (2012-11-19 21:59:59+00:00)
Title: Preventing a NoMethodError when calling #as_document on an embedded relation that is nil.

If you have an `embeds_one` relationship that at some point gets set to nil, calling #as_document on the model will cause a NoMethodError. I ran into this where my destroy callback ended up calling #as_document and I got this error:

```ruby
p = Person.new(title: "Sir")
p.build_name(first_name: "James")
p.save
p.name = nil
p.save
p.reload

# This line will throw a NoMethodError
p.as_document
```

This patch fixes it and adds a spec.

Also, in doing this, I realized that the "as_document" doesn't update until the document is re-pulled from Mongo. That is, in my above example if you don't call `p.reload`, then `p.as_document` will return `{ "name" : {"first_name" : "James" } }` even if the document is saved. @durran, is this expected behavior? Seems like a bug but wanted to ask you first.