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

embeds_one with autobuild raise a FrozenError after destroy

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 8.0.8
    • Component/s: Associations
    • None
    • None
    • Ruby Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      When using Mongoid 8, a FrozenError is raised if a relation is created with embeds_one autobuild: true and the model is subsequently deleted.

      Steps to Reproduce

      Here is the code snippet to reproduce the issue:

      require 'bundler/inline'
      
      gemfile do
        source 'https://rubygems.org'
      
        gem 'mongoid', '~> 8.0.8'
        # gem 'mongoid', '~> 7.5.4'
      end
      
      Mongoid.configure do |config|
        config.clients.default = {
          hosts: ['172.18.0.4:27017'],
          database: 'mongoid_test',
        }
      end
      Mongoid.purge!
      
      class Article
        include Mongoid::Document
      
        embeds_one :intro, autobuild: true # No error with `embeds_one :intro` only
      end
      
      class Intro
        include Mongoid::Document
      
        embedded_in :article
      end
      
      article = Article.create!
      article.destroy
      pp article.intro # It should be nil but raise a Frozen Error
      

       

      Here is the backtrace of the error:

      /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/embedded/embeds_one/proxy.rb:128:in `merge!': can't modify frozen Hash: {"_id"=>BSON::ObjectId('678817aa4c916802771d3999')} (FrozenError)
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/embedded/embeds_one/proxy.rb:128:in `update_attributes_hash'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/embedded/embeds_one/proxy.rb:34:in `block in initialize'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/proxy.rb:55:in `init'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/embedded/embeds_one/proxy.rb:30:in `initialize'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/relatable.rb:256:in `new'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/relatable.rb:256:in `create_relation'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/accessors.rb:46:in `create_relation'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/accessors.rb:27:in `__build__'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/accessors.rb:351:in `block (3 levels) in define_setter!'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/accessors.rb:243:in `without_autobuild'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/accessors.rb:342:in `block (2 levels) in define_setter!'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/builders.rb:51:in `block (3 levels) in define_builder!'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/threaded/lifecycle.rb:80:in `_building'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/builders.rb:50:in `block (2 levels) in define_builder!'
              from /usr/local/bundle/gems/mongoid-8.0.8/lib/mongoid/association/accessors.rb:302:in `block (2 levels) in define_getter!'
              from tmp/mongoid.rb:35:in `<main>'
      

      Note that this raise no error :

      article = Article.create!
      article.intro
      article.destroy
      pp article.intro
      

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            guirec.corbel@gmail.com Guirec Corbel
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              None
              None
              None
              None