Run a rake db:seed multiple times without creating duplicate records?

Viewed 3320

I have some code in a seed file that I'd like to alter so that it does not create duplicate records when I run the seed command multiple times. Any way to adapt the code below from my seeds file so that this is possible? The find_or_create_by method doesn't appear to work here unless I am mistaken.

data_file = Rails.root.join('db/data/data.csv')

CSV.foreach(data_file) do |row|
  TownHealthRecord.create(
    city: row[0],
    state: row[1],
    country: row[2],
    zip_code: row[3],
    area_code: row[4]
    )
end
1 Answers
Related