Delete by id in mongoid

Is there any command in Mongoid to delete an object using id?

Something like,

ClassName.delete(:id)

      

I currently don't see anything like this and use im,

obj = ClassName.find(:id)
obj.delete

      

What could be better?

+3


source to share


2 answers


You can do something like the following:

ClassName.delete_all(conditions: { _id: BSON::ObjectId("whatevertheidis")})

      



You need to underline the symbol _id

or it won't work.

Also, it may not matter, but destroy_all

will trigger the callback methods of the model and delete_all

not.

+2


source


Another way



ClassName.any_in(:_id => ["id1", "id2"]).destroy_all

      

+3


source







All Articles