mongoid/mongoid — Issue #1357: update_attributes doesn't clear embedded documents for another embedded document
Author: gorkunov
Created: 2011-10-19 13:12:36+00:00
State: open

Mongoid: 2.2.3
Ruby: ree-1.8.7
Rails: 3.0.10

Simular with issue #1225.
This case works fine:

```ruby
class Post
include Mongoid::Document
embeds_many :comments
end

class Comment
include Mongoid::Document
field :text, :type => String
embedded_in :post
end

Post.delete_all
Post.create!(:comments => [{ :text => 'message 1' }])
p = Post.first
p.update_attributes :comments => [{ :text => 'message 2' }]

Post.first.comments.count
=> 1
```
But this works incorrectly:

```ruby
class Blog
include Mongoid::Document
embeds_many :posts
end
class Post
include Mongoid::Document
embedded_in :blog
embeds_many :comments
end

class Comment
include Mongoid::Document
field :text, :type => String
embedded_in :post
end

Blog.delete_all
Blog.create!({ :posts => [{ :comments => [{ :text => 'message 1' }] }] })
p = Blog.first.posts.first
p.update_attributes :comments => [{ :text => 'message 2' }]
reload!
Blog.first.posts.first.comments.count
=> 2
```