1 min read

Ruby on Rails: replant = truncate + seed

Ruby on Rails db:seed command can
Ruby on Rails: replant = truncate + seed
Photo by set.sj / Unsplash

Looking to regularly reset and reseed a database in an environment for your Ruby on Rails application?

Well, there is no need to drop and recreate the database. If you are just looking for a way to empty it and reseed it cleanly, rails db:seed:replant will take care of that by truncating the tables before seeding them.

  namespace :seed do
    desc "Truncates tables of each database for current environment and loads the seeds"
    task replant: [:load_config, :truncate_all, :seed]
  end
  # ...
  # desc "Truncates tables of each database for current environment"
  task truncate_all: [:load_config, :check_protected_environments] do
    ActiveRecord::Tasks::DatabaseTasks.truncate_all
  end

That's handy as the user used within the database might not have enough rights to create and drop databases in some environments.