refactor: update city sync logic with country and state

- Replace country name lookup with country code lookup
- Replace state name lookup with state code lookup
- Change city creation to ensure country association

This update modifies the existing logic for synchronizing city data to
use country and state codes instead of names. This aids in ensuring
more robust data integrity by relying on unique identifiers.
This commit is contained in:
songtianlun 2025-02-11 10:35:41 +08:00
parent b4eac06227
commit 24597e6320

View File

@ -161,10 +161,12 @@ namespace :geo do
count = 1
cities.each do |data|
city = City.find_or_initialize_by!(name: data["name"])
country = Country.find_by!(code: data["country_code"])
state = State.find_by(code: data["state_code"])
country = Country.find_by(name: data["country_name"])
state = State.find_by(name: data["state_name"])
city = City.find_or_create_by!(name: data["name"]) do |c|
c.country_id = country.id
end
puts "Syncing City[#{count}/#{sum}] [#{data["name"]}] Country:[#{country&.name}] "
count += 1