diff --git a/lib/tasks/sync_geo_data.rake b/lib/tasks/sync_geo_data.rake index 482e191..ae5204e 100644 --- a/lib/tasks/sync_geo_data.rake +++ b/lib/tasks/sync_geo_data.rake @@ -63,7 +63,6 @@ namespace :geo do count = 1 countries.each do |data| - # puts "Syncing countries for #{data["name"]}" # 处理 Region region = if data["region_id"] Region.find_by(id: data["region_id"]) @@ -125,12 +124,22 @@ namespace :geo do puts "Syncing State[#{count}/#{sum}] [#{data["name"]}] country:[#{data["country_name"]}]" count += 1 + country = + if data["country_code"] + Region.find_by(id: data["country_code"]) + elsif data["country_name"] + Region.find_by(name: data["country_name"]) + else + puts "Skip states #{data["name"]} without country" + next + end + state = State.find_or_create_by!(name: data["name"]) do |s| - s.country_id = data["country_id"] + s.country_id = country.id end state.update!( - country_code: data["country_code"], + country_code: country.code, fips_code: data["fips_code"], iso2: data["iso2"], code: data["state_code"],