Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Destroy multiple objects simultaneously in Rails 3

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.

like image 510
Cyber Avatar asked Jan 21 '13 07:01

Cyber


People also ask

What is the difference between delete and destroy in rails?

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.

How do you destroy in rails?

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.

What is dependent destroy in rails?

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.


1 Answers

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 
like image 193
Eugene Rourke Avatar answered Sep 23 '22 15:09

Eugene Rourke