I would like to have a rake task for truncating all the tables. I have found one on the internet, but it is supposed only for Rails 2 and does not work for Rails 3 (problem is in getting a database connection).
rake db:reset is not an option, because I am using PostgreSQL and it also drops the user. Therefore migration fails. I only want to clear the data.
Do you guys have somehting for me?
I’ve found this via google, and then I got a much simpler solution than the one approved, so here it is: Use the database_cleaner gem. Here’re the steps.
In your Gemfile (execute bundle after modifying):
With that gem in place, the statement
DatabaseCleaner.clean_with :truncationwill truncate the database. Adding it to a rake task is trivial:That’s it. You can also use the
DatabaseCleaner.clean_with :truncationline inside yourdb/seeds.rbfile directly so that you don’t forget to truncate the database before seeding.