Skip to content

Instantly share code, notes, and snippets.

@changemewtf
Created October 16, 2014 20:40
Show Gist options
  • Save changemewtf/ec3d7aaf0378938e27db to your computer and use it in GitHub Desktop.
Save changemewtf/ec3d7aaf0378938e27db to your computer and use it in GitHub Desktop.
Auto-clear database on rake db:seed
# db/seed.rb
# This block will automatically empty the database every time db:seed is run.
ActiveRecord::Base.connection.tables.each do |table|
# Don't clear our migration history! Clear everything else.
if table != 'schema_migrations'
# http://stackoverflow.com/a/7758797
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table};")
# http://stackoverflow.com/questions/2097052/rails-way-to-reset-seed-on-id-field
ActiveRecord::Base.connection.reset_pk_sequence! table
end
end
# Actual seed data creation goes here...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment