Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple records from Rails console

I'm trying to delete multiple records in my database from the Rails console. I know i can do User.find(1).destroy. What if I want to delete all users with ids 100 - 200. Is there a way to do this in the console?

like image 231
Sara Avatar asked Apr 01 '13 02:04

Sara


People also ask

How to delete a record from rails console?

Rails Delete operation using delete method Unlike the destroy method, with delete, you can remove a record directly from the database.

How to destroy record 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.

How delete all data from table in rails?

If you are looking for a way to it without SQL you should be able to use delete_all. See here for more information. The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.


1 Answers

You can do something like User.delete_all("id > 100 AND id < 200").

like image 102
Dan Wich Avatar answered Sep 18 '22 09:09

Dan Wich