refactor: streamline geo data syncing process

- Remove unnecessary debug output for country syncing.
- Improve country determination logic by adding checks for
  both country code and country name.
- Ensure state records are associated with the correct
  country ID post-refactor.

These changes enhance the clarity and efficiency
of the geo data synchronization code, making it easier to
maintain and reducing the risk of errors during data
syncing.
This commit is contained in:
songtianlun 2025-02-11 10:30:25 +08:00
parent febee58d0a
commit b4eac06227

View File

@ -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"],