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

Embedded object when deleted using delete_all, some of it still exist.

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • 6.1.1
    • Affects Version/s: None
    • Component/s: None
    • None

      I use mongoid version 4.0.0.beta1

      Here is my model:

      class Project
      ...
      embeds_many :bim_objects
      has_many :model_containers, inverse_of: :project, autosave: true
      ...

      class ModelContainer
      ...
      belongs_to :project, inverse_of: :model_containers, autosave: true

      field :bim_object_ids, type: Array, default: ->

      { [] }

      ...

      In other model, I need to delete only some bim_objects from model_container object

      model_container.project.bim_objects.in(id: model_container.bim_object_ids).all.delete_all
      model_container.bim_object_ids = []
      ...
      #build new bim_object

      delete_all not working, and I got duplicated bim_objects, while it should delete all old bim_objects and store new ones.

      I also add a callback in ModelContainer to delete all bim_objects that it's refer:

      before_destroy :destroy_bim_objects

      def destroy_bim_objects
      unless bim_object_ids.empty?
      project.bim_objects.in(id: bim_object_ids).all.delete_all
      end
      end

      Somehow, if there are 25 bim_objects, only 23 got deleted.

      Here also the result from console:

      2.0.0-p353 :136 > project.bim_objects.count
      => 27
      2.0.0-p353 :137 > project.bim_objects.count
      => 27
      2.0.0-p353 :138 > project.bim_objects.delete_all
      => 27
      2.0.0-p353 :139 > project.reload
      2.0.0-p353 :140 > project.bim_objects.count
      => 9
      2.0.0-p353 :141 > project.bim_objects.delete_all
      => 9
      2.0.0-p353 :142 > project.reload
      2.0.0-p353 :143 > project.bim_objects.count
      => 9
      2.0.0-p353 :144 > project.bim_objects.collect(&:destroy!)
      => [true, true, true, true, true]
      2.0.0-p353 :145 > project.reload
      2.0.0-p353 :146 > project.bim_objects.count
      => 4

      Somehow, there are objects that stubborn and refuse to be destroyed. MOPED log show no errors, $pullAll query executed as expedted, but some objects still embedded into project.

      I have review other issues, even follow this blog post:
      http://bashar3a.com/2013/12/05/deleting-and-destroying-embedded-documents-in-mongoid-with-rails-4/

      Any clue what did I dealing with? Will changing embedded into referenced solve this problems?

      Thanks,

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            donnykurnia donnykurnia
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: