refactor: simplify country retrieval logic

- Replace verbose conditional checks with a ternary operator
- Use 'find_by!' for better error handling if country not found
- Maintain the functionality of state creation associated with the country
This commit is contained in:
songtianlun 2025-02-11 10:50:08 +08:00
parent 24597e6320
commit 90c491637b

View File

@ -125,14 +125,9 @@ namespace :geo do
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
data["country_code"] ?
Country.find_by!(code: data["country_code"]) :
Country.find_by!(name: data["country_name"])
state = State.find_or_create_by!(name: data["name"]) do |s|
s.country_id = country.id