update_attributes doesn't clear embedded documents for another embedded document

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • 2.3.4
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Mongoid: 2.2.3
      Ruby: ree-1.8.7
      Rails: 3.0.10

      Simular with issue MONGOID-1226.
      This case works fine:

      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:

      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
      

              Assignee:
              Unassigned
              Reporter:
              gorkunov
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved: