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?
Rails Delete operation using delete method Unlike the destroy method, with delete, you can remove a record directly from the database.
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.
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.
You can do something like User.delete_all("id > 100 AND id < 200")
.
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