How to delete all data from all tables in Rails?

前端 未结 16 2509
情歌与酒
情歌与酒 2020-12-07 15:23

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

16条回答
  •  天涯浪人
    2020-12-07 16:03

    This will work also for Rails 4

    (ActiveRecord::Base.connection.tables - ['schema_migrations']).each do |table|
        table.classify.constantize.destroy_all
    end
    

提交回复
热议问题