user14069773
user14069773

Reputation: 175

Rails - Destroy all records

Is there a way to destroy all records in my database in one line, without specifying my models?

Say I have three models User Picture Post. I can call User.all.destroy_all etc, but can I collect all records without specifying the models themselves?

Upvotes: 0

Views: 80

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

As Sebastian Palma says you can do the rake task rake db:reset which will drop and setup your database.

Alternatively you can get all the descendants of ActiveRecord. If you're in development mode you'll need to eager_load first.

Rails.application.eager_load!

Then you could do

ActiveRecord.descendants.each(&:destroy_all)

PLEASE BE EXTREMELY CAREFUL! THE ABOVE WILL DELETE ALL RECORDS IN YOUR DATABASE TABLES!

Upvotes: 3

Related Questions