是否有可能测试是否已经存在具有相同名称的元素?测试名称是否已被使用
假设我有一个数组,像这样: lregion=["de", "eu", "us", "it"]
我希望把他们在我的表与此脚本,然后,如果已经有名为“欧盟”的元素不应该创建再次。
lregion.each do |x|
if #this "actually" should test, if there is already a country with this countrycode
Country.create(countrycode: x) #creates Country
end
end
做这样是很重要的,因为我使用这个脚本在我的种子文件,并不能只用uniqueness: true
在我的模型,因为它将使seeds.rb崩溃。
有没有人有我的问题的想法?
验证唯一性并拯救错误或使用first_or_create –