Truncate table(s) with rails console

前端 未结 9 1339
北恋
北恋 2021-02-02 05:51

I have this testingdatabase which, by now, is stuffed with junk. Now I\'ve done a few Table.destroy_all commands in the rails console which deletes all records and dependencies

相关标签:
9条回答
  • 2021-02-02 06:29
    Model.connection.truncate(Model.table_name)
    
    0 讨论(0)
  • 2021-02-02 06:31

    rake db:reset will perform rake db:drop db:setup. In other words, drop the database and setup the database again.

    Source

    0 讨论(0)
  • 2021-02-02 06:34

    (A bit late to the party, I know)

    Asking to do this in the console:

    2.1.2 :001 > Post.all.each do |post|
    2.1.2 :002 >   post.destroy!
    2.1.2 :003 > end
    

    Works as well...

    This essentially loops through all of the posts and destroys them. It does not change your auto increment value though...

    Same logic should work for Rails 3 as well (though I am using Rails 4)

    0 讨论(0)
提交回复
热议问题