p8_chw
Last Updated: February 25, 2016
·
2.014K
· sergiotapia
Avatar mario

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.

Say Thanks
Respond

2 Responses
Add your response

8948
Me

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

over 1 year ago ·
8957
Avatar mario

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

over 1 year ago ·