I have a Rails application that is trying to delete multiple objects at a time.
I have tried like sending set of id seperated by ',' to rails destroy method,but it destroy only single object. Is it possible to delete multiple objects in rails 3.
Basically destroy runs any callbacks on the model while delete doesn't. Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted). Returns the frozen instance.
Rails delete operation using destroy methodBy using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.
Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.
destroy_all destroys the records matching conditions by calling destroy method for each instantiating record. So object’s callbacks are executed.
Model.destroy_all(:status => "inactive") Model.where(:id => [1,2,3,4,5]).destroy_all Model.where(:id => 1..5).destroy_all
UPDATE
User.where(:id => params[:ids]).destroy_all /users?ids[]=1&ids[]=2&ids[]=3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With