Skip to content

Empty objects when destroying multiple documents. #9

Closed
mattiassvedhem opened this Issue Sep 14, 2012 · 4 comments

2 participants

@mattiassvedhem

So when I do something like this on a model that has orderable scoped to it's parent:

Place.first.photos.all.each { |p| p.destroy }
MONGODB (0ms) app['places'].update({"_id"=>BSON::ObjectId('4f99f0eea2b279ec3d000006')}, {"$pull"=>{"photos"=>{"_id"=>BSON::ObjectId('50531aae66eaa76979000001')}}})
MONGODB (0ms) app['places'].update({"_id"=>BSON::ObjectId('4f99f0eea2b279ec3d000006'), "photos._id"=>BSON::ObjectId('50531b1166eaa76979000002')}, {"$inc"=>{"photos.1.position"=>-1}})
MONGODB (0ms) app['places'].update({"_id"=>BSON::ObjectId('4f99f0eea2b279ec3d000006')}, {"$pull"=>{"photos"=>{"_id"=>BSON::ObjectId('50531b1166eaa76979000002')}}})

Then what I'm left with is (note: position is decremented).

=> #<Photo _id: , _type: nil, width: nil, height: nil, crop_x: "", crop_y: "", crop_h: "", crop_w: "", title: nil, image: nil, position: -1>

This is due to the fact that remove_from_list is implemented like this:

def remove_from_list
    orderable_scoped.where(orderable_column.gt => orderable_position).inc(orderable_column, -1)
end

And I guess that somehow that command conflicts with the delete that happens directly after the atomic operation.

Note, that in reality I'm not looping through the photos, The issue happens when I hit DELETE to /photos multiple times on each of the photos (it's in an API).

Do you have any Idea why this is happening? and do you have a solution?

Thanks a lot.

@pyromaniac
Owner

Can you give me example models source please?

@mattiassvedhem
class Restaurant
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :photos
end
class RestaurantPhoto
  include Mongoid::Document
  include Mongoid::Orderable

  embedded_in :restaurant
  orderable
end

So when destroying the photos it does a $pull on the array but then somehow I'm left with one or two incremented but otherwise empty photos depending on how many photos there were from start.

I'm running Mongoid 2.4.

@pyromaniac
Owner

Nope. I can not reproduce the bug. Can you describe your actions more precisely? I need a source for repl, which reproduces this bug. Can you provide it?

@mattiassvedhem

I'll try to extract an example from our application. Thanks!

@pyromaniac pyromaniac closed this Jan 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.