Last Updated: February 25, 2016
·
2.617K
· sergiotapia

Run seeds.rb file only against Development.

In my Rails seeds file I sometimes have the need to destroy all records for a given model. This is extremely dangerous in production. One key slip and you could potentially trash the entire database and then you'd need to restore from a backup. An entire morning wasted.

Here's a simple trick to only run destructive seed instructions in Development.

case Rails.env
when 'development'
  Person.delete_all
  Address.delete_all

  Person.create!(name: 'Sergio Tapia')
end

No more botched seed runs.

2 Responses
Add your response

Do not use #create in seeds.rb. Use #create! to be sure that your models are created.

over 1 year ago ·

Good idea! I agree. I'll edit the tip.

over 1 year ago ·