I can do Post.delete_all
to delete all my posts, but what if I want to delete all posts, comments, blogs, etc.?
How do I iterate over all my models and run the delete_all
method?
Rails Delete operation using delete method Unlike the destroy method, with delete, you can remove a record directly from the database. Any dependencies to other records in the model are not taken into account. The method delete only deletes that one row in the database and nothing else.
rake db:reset
It recreates your table from migrations.
As suggested in the comments, a faster way to do it (but you have to add a new rake task) is:
namespace :db do desc "Truncate all tables" task :truncate => :environment do conn = ActiveRecord::Base.connection tables = conn.execute("show tables").map { |r| r[0] } tables.delete "schema_migrations" tables.each { |t| conn.execute("TRUNCATE #{t}") } end end
Response copied from: answer on SO.
You can have finer control with:
rake db:drop:all
And then create the database without running the migrations,
rake db:create:all
Then run all your migrations,
rake db:migrate
You can also do:
mysqladmin drop databasename
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