task :countries => :environment do
file = Rails.root.join('import', 'wikibased', 'countries.csv').to_s
CSV.foreach(file, headers: true) do |row|
puts "import #{row['name']}"
# Проверка, существует ли уже в базе данных эта сущность?
# Проверка по id
if row["_id"] && row["_id"] !='' && Country.where(id: row["_id"].strip).count > 0
tCountry = Country.find(row["_id"].strip)
# Проверка по name
elsif Country.where(name: row["name"].strip).count > 0
tCountry = Country.find_by(name: row["name"].strip)
# Проверка по engname
elsif Country.where(engname: row["engname"].strip).count > 0
tCountry = Country.find_by(engname: row["engname"].strip)
# Если все-же такой сущности нет - создаём новую
else tCountry = Country.new
end
tCountry[:name] = (row["name"] ? row["name"].strip : nil)
tCountry[:engname] = (row["engname"] ? row["engname"].strip : nil)
tCountry[:codeFifa] = (row["codeFifa"] ? row["codeFifa"].strip : nil)
tCountry[:fifaStatus] = (row["fifaStatus"] ? row["fifaStatus"].strip : nil)
tCountry[:code3] = (row["code3"] ? row["code3"].strip : nil)
tCountry[:code2] = (row["code2"] ? row["code2"].strip : nil)
tCountry[:realCountry] ||= false
tCountry['imported'] << "wikibased"
# tCountry[:federation_id] = (row["federation_id"] ? row["federation_id"].strip : nil)
tCountry.save!
end
end