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

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

    XMLWordPrintable

    Details

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

      Description

       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.

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: