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

Saving embedded associations generate incorrect sets

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 8.0.5, 8.1.5
    • Component/s: Associations
    • None
    • Ruby Drivers

      require 'byebug'
      require 'mongoid'
      
      require 'minitest/autorun'
      
      Mongoid.configure do |config|
        config.connect_to('mongoid_embedded_test')
      end
      
      Mongo::Logger.logger = Logger.new(STDOUT)
      Mongo::Logger.logger.level = Logger::DEBUG
      # increase log size limit
      Mongo::Monitoring::CommandLogSubscriber::LOG_STRING_LIMIT = 10_000
      
      ###
      # Models
      ###
      
      class Post
        include Mongoid::Document
      
        field :title, type: String
      
        embeds_one :extra
      end
      
      class Extra
        include Mongoid::Document
      
        field :info, type: String
      
        embedded_in :post
        embeds_many :tags
      end
      
      class Tag
        include Mongoid::Document
      
        field :label, type: String
      
        embedded_in :extra
      end
      
      ###
      # Test
      ###
      
      class TestUpdateEmbedded < Minitest::Test
        def setup
          Post.delete_all
        end
      
        def test_assigning_embedded
          post = Post.create(title: 'foo')
      
          extra = Extra.new(info: 'bar')
          tags = [Tag.new(label: 'tag'), Tag.new(label: 'tag2')]
      
          extra.assign_attributes(tags: tags)
          post.assign_attributes(extra: extra)
      
          post.save!
      
          assert_nil post.reload.as_document['tags']
        end
      end
      

      This issue was caused by this: https://github.com/mongodb/mongoid/pull/5104. I think this needs to use the introduced concepts of 'descendants' too: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/association/embedded/batchable.rb#L138

            Assignee:
            Unassigned Unassigned
            Reporter:
            rodrigorcomp@gmail.com Rodrigo R Aquino
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: