how to clear whole database in Rails seeds.rb

Viewed 24345

What is the best way to accomplish this? As for now I'm using:

Role.delete_all
User.delete_all
...

but how to clear habtm talbes? Like roles_users

Updated Answer

I think ream88 response answers my question most precisely, but probably the bestidea is to follow coreyward suggestion to use separate rake tasks and leave seeds.rb only for seeding data.

This is updated answer from ream88 which doesn't delete schema_migrations table.

ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.tables.each do |table|
  # MySQL
  ActiveRecord::Base.connection.execute("TRUNCATE #{table}") unless table == "schema_migrations"

  # SQLite
  # ActiveRecord::Base.connection.execute("DELETE FROM #{table}") unless table == "schema_migrations"
end

Thanks a lot for help!

3 Answers
Related