Truncate table(s) with rails console

前端 未结 9 1514
北恋
北恋 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:23

    Assuming you're using MySQL or Postgre and not SQlite3 (which doesn't support TRUNCATE), you could do the following:

    MyModel.connection_pool.with_connection { |c| c.truncate(MyModel.table_name) }
    

    Note that this would not invoke ActiveRecord callbacks.

提交回复
热议问题