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

Re-associating identical object destroys itself for dependent: :destroy

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.2.0, 7.1.6
    • Affects Version/s: 7.1.2, 7.1.4
    • Component/s: Associations
    • Labels:
      None
    • Environment:
      MongoDB 4.0
    • Fully Compatible

       Here is a reproducible code. 

      require 'bundler/inline'
      require 'tempfile'
      
      gemfile do
        source 'https://rubygems.org'
        gem 'mongoid', '7.1.4'
      end
      
      Tempfile.open(%w(config.yml)) do |f|
        f.write(<<~YAML)
      development:
        clients:
          default:
            uri: mongodb://mongo/test
        YAML
        f.flush
      
        Mongoid.load!(f.path, 'development')
      end
      
      class Parent
        include Mongoid::Document
        include Mongoid::Timestamps
      
        has_one :child, dependent: :destroy
      end
      
      class Child
        include Mongoid::Document
        include Mongoid::Timestamps
      
        belongs_to :parent
      end
      
      parent = Parent.create
      child = Child.create(parent: parent)
      p Child.find(child.id) #=> returns the child
      
      parent.child = child
      p Child.find(child.id) #=> raises Mongoid::Errors::DocumentNotFound
      

      This also happens with a polymorphic association. This doesn't happen without the dependent: :destroy option.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            masato.ikeda@quipper.com Masato Ikeda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: