How to delete all data from all tables in Rails?

前端 未结 16 2495
情歌与酒
情歌与酒 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:22

    You could list all the models in the seed-file (seeds.rb), and simply run

    rake db:seed
    

    The seed file would then look something like this:

    Model1.delete_all
    Model2.delete_all
    Model3.delete_all
    Model4.delete_all
    Model5.delete_all
    Model6.delete_all
    Model7.delete_all
    

    ...

    rake db:reset is too much for your job here. That will completely kill off your database and rebuild it from scratch, running all migrations etc. To run the seed command is faster.

提交回复
热议问题