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

Destroy not working for embedded documents

      For some reason deletions of embedded documents is not working in the situation below:

      class Car
        include Mongoid::Document
        include Mongoid::Timestamps::Created
        field :name, type: String
        #...
        embeds_many :car_images, cascade_callbacks: true
      end
      
      class CarImage
        include Mongoid::Document
        include Mongoid::Timestamps::Created
        field :main, type: Boolean, default:false
        mount_uploader :image, CarImageUploader, mount_on: :image_filename
        validates :image, :file_size => {:maximum => 5.megabytes.to_i}, :allow_blank => false
        embedded_in :car
      end
      

      Ran the following:

      Car.find_by(name:'Susie').car_images
      

      I get the following:

      [
          [0] #<CarImage:0x007f821a372aa0> {
                   :_id => "511458085d39f373cb00001f", :main => false
          },
          [1] #<CarImage:0x007f821a371f38> {
                   :_id => "511458085d39f373cb000020", :main => false
          },
          [2] #<CarImage:0x007f821a3795f8> {
                   :_id => "511458085d39f373cb000021", :main => false
          },
          [3] #<CarImage:0x007f821a378540> {
                   :_id => "511458085d39f373cb000022", :main => true
          }
      ]
      
      Car.find_by(name:'Susie').car_images.destroy_all
      4
       => nil 
      
      Car.find_by(name:'Susie').car_images.first.destroy
      true
       => nil 
      

      Why does it show true and gives me 4 when no deletions were made?

      test_delete = Car.find_by(name:'Susie').car_images.first.destroy
      test_delete.errors
       => #<ActiveModel::Errors:0x007f821a2f9100 @base=#<CarImage _id: 511458015d39f373cb00001e, _type: nil, created_at: nil, main: false, image_filename: nil>, @messages={}> 
      

      I'm using mongoid 3.0.21

      Thanks!

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

              Created:
              Updated:
              Resolved: