我在种子文件中有一些我想更改的代码,以便在我多次运行种子命令时它不会创建重复记录。有什么方法可以从我的种子文件中修改下面的代码,这样就可以了?除非我弄错了,否则 find_or_create_by 方法似乎在这里不起作用。
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