diff --git a/lib/tasks/sync_geo_data.rake b/lib/tasks/sync_geo_data.rake index 1e39a0c..cb7aa9e 100644 --- a/lib/tasks/sync_geo_data.rake +++ b/lib/tasks/sync_geo_data.rake @@ -22,11 +22,11 @@ namespace :geo do count = 1 regions.each do |data| - region = Region.find_or_create_by(name: data["name"]) + region = Region.find_or_create_by!(name: data["name"]) puts "Sync Regions[#{count}/#{sum}]: [#{region.name}]" count += 1 - region.update( + region.update!( code: data["name"], translations: data["translations"], flag: data["flag"] || true, @@ -42,13 +42,13 @@ namespace :geo do count = 1 subregions.each do |data| - subregion = Subregion.find_or_initialize_by(name: data["name"]) do |s| + subregion = Subregion.find_or_create_by!(name: data["name"]) do |s| s.region_id = data["region_id"] end puts "Sync Subregions[#{count}/#{sum}]: [#{subregion.name}]" count += 1 - subregion.update( + subregion.update!( translations: data["translations"], flag: data["flag"] || true, wiki_data_id: data["wikiDataId"] @@ -82,13 +82,11 @@ namespace :geo do count += 1 # 查找或初始化 Country - country = Country.find_by(code: data["iso2"]) - if country.nil? - country = Country.new - end + country = Country.find_or_create_by!(code: data["iso2"], name: data["name"]) # 更新 Country 属性 - country.update( + country.update!( + name: data["name"], iso3: data["iso3"], numeric_code: data["numeric_code"], iso2: data["iso2"], @@ -110,8 +108,8 @@ namespace :geo do flag: data["flag"] || true, wiki_data_id: data["wikiDataId"] ) - country.update(region: region) if region != nil - country.update(subregion: subregion) if subregion != nil + country.update!(region: region) if region != nil + country.update!(subregion: subregion) if subregion != nil end end @@ -125,11 +123,11 @@ namespace :geo do puts "Syncing State[#{count}/#{sum}] [#{data["name"]}] " count += 1 - state = State.find_or_initialize_by(name: data["name"]) do |s| + state = State.find_or_initialize_by!(name: data["name"]) do |s| s.country_id = data["country_id"] end - state.update( + state.update!( country_code: data["country_code"], fips_code: data["fips_code"], iso2: data["iso2"], @@ -152,7 +150,7 @@ namespace :geo do count = 1 cities.each do |data| - city = City.find_or_initialize_by(name: data["name"]) + city = City.find_or_initialize_by!(name: data["name"]) country = Country.find_by(name: data["country_name"]) state = State.find_by(name: data["state_name"]) @@ -160,7 +158,7 @@ namespace :geo do puts "Syncing City[#{count}/#{sum}] [#{data["name"]}] Country:[#{country&.name}] " count += 1 - city.update( + city.update!( latitude: data["latitude"], longitude: data["longitude"], flag: data["flag"] || true, @@ -172,10 +170,10 @@ namespace :geo do country_code: country.code, ) end - city.update(state: state) unless state == nil - city.update(state_code: state.code) unless state == nil - city.update(active: false) if city.active == nil - city.update(priority: 10) if city.priority == nil + city.update!(state: state) unless state == nil + city.update!(state_code: state.code) unless state == nil + city.update!(active: false) if city.active == nil + city.update!(priority: 10) if city.priority == nil end end end