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

Fix assign_attributes on replacing embeds_many

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.1, 7.4.1, 7.3.5, 7.5.3
    • Affects Version/s: 7.3.4
    • Component/s: None
    • Labels:
      None
    • Fully Compatible

      Setup:

       

      class Post
        include Mongoid::Document
      
        embeds_many :company_tags
        embeds_many :user_tags
      end
      
      
      class CompanyTag
        include Mongoid::Document
      
        field :title, type: String
      
        embedded_in :post
      end
      
      
      class UserTag
        include Mongoid::Document
      
        field :title, type: String
      
        embedded_in :post
      end
      

      Initialization:

       

       

      post = Post.create
      post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'a'}],
                             user_tags: [{id: BSON::ObjectId.new, title: 'b'}])
      post.save
      post.reload
      p post.company_tags # [#<CompanyTag .., title: "a">]
      p post.user_tags # [#<UserTag .., title: "b">] 

      7.3.3:

       

       

      post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'c'}],
                             user_tags: [])
      post.save
      p post.company_tags # [#<CompanyTag .., title: "c">] CORRECT
      p post.user_tags # [] CORRECT
      post.reload
      p post.company_tags # [#<CompanyTag .., title: "c">] CORRECT
      p post.user_tags # [] CORRECT 

      7.3.4:

       

      post.assign_attributes(company_tags: [{id: BSON::ObjectId.new, title: 'c'}],
                             user_tags: [])
      post.save
      p post.company_tags # [#<CompanyTag .., title: "c">] CORRECT
      p post.user_tags # [] CORRECT
      post.reload
      p post.company_tags # [#<CompanyTag .., title: "a">, #<CompanyTag .., title: "c">] WRONG
      p post.user_tags # [] CORRECT 

      Queries:

      # 7.3.3
      # "u"=>{"$unset"=>{"user_tags"=>true}, "$set"=>{"company_tags"=>[{"_id"=>BSON::ObjectId('627d54d358af14bf60afa0b9'), "title"=>"c"}]}}
      
      # 7.3.4
      # "u"=>{"$unset"=>{"user_tags"=>true}, "$push"=>{"company_tags"=>{"$each"=>[{"_id"=>BSON::ObjectId('627d555558af14c2327e7592'), "title"=>"c"}]}}} 

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            shark55555@gmail.com Alexey Ramazanov
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: