fix: improve city creation with country and state codes

- Update city creation to include country and state codes
- Ensure that the country and state are fetched correctly
- Use safe navigation to avoid nil errors

This change enhances the city creation process by ensuring that
country and state codes are properly associated with the city,
improving data integrity and consistency.
This commit is contained in:
songtianlun 2025-02-26 11:04:39 +08:00
parent bdabf49626
commit b3154c64e8

View File

@ -164,7 +164,11 @@ namespace :geo do
country = Country.find_by!(code: data["country_code"])
state = State.find_by(code: data["state_code"])
city = City.find_or_create_by!(name: data["name"]) do |c|
city = City.find_or_create_by!(
name: data["name"],
country_code: country&.code,
state_code: state&.code,
) do |c|
c.country_id = country.id,
c.country_code = country.code,
c.latitude = data["latitude"],