I can do Post.delete_all to delete all my posts, but what if I want to delete all posts, comments, blogs, etc.?
Post.delete_all
How do I iterate over all my models and run the delete_all method?
delete_all
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.
2.1m questions
2.1m answers
60 comments
57.0k users