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

Correct detach previous relation when new one is assigned

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.10
    • Affects Version/s: None
    • Component/s: None
    • None

      This commit fixes the following error:

      class Door
        include Mongoid::Document
        has_one :door_key
      end
      
      class DoorKey
        include Mongoid::Document
        belongs_to :door
      end
      
      door = Door.new(door_key: DoorKey.new)
      door.save && door.door_key.save
      door.reload # now model doesn't know about its relation object
      door.door_key = DoorKey.new # previous model will not be detached
      
      door = Door.new(door_key: DoorKey.new)
      door.save && door.door_key.save
      door.reload
      door.door_key # now Door model knows about door_key relation
      door.door_key = DoorKey.new # previous model will be detached correctly
      
      # Collection dump:
      db.door_keys.find()
      { "_id" : ObjectId("507b22b2ea6683af09000002"), "door_id" : ObjectId("507b22b2ea6683af09000001") }
      { "_id" : ObjectId("507b22b2ea6683af09000003"), "door_id" : ObjectId("507b22b2ea6683af09000001") }
      { "_id" : ObjectId("507b22b2ea6683af09000005"), "door_id" : null }
      { "_id" : ObjectId("507b22b2ea6683af09000006"), "door_id" : ObjectId("507b22b2ea6683af09000004") }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            kostyantyn kostyantyn
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: