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:
parent
24597e6320
commit
90c491637b
@ -125,14 +125,9 @@ namespace :geo do
|
|||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
country =
|
country =
|
||||||
if data["country_code"]
|
data["country_code"] ?
|
||||||
Region.find_by(id: data["country_code"])
|
Country.find_by!(code: data["country_code"]) :
|
||||||
elsif data["country_name"]
|
Country.find_by!(name: 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|
|
state = State.find_or_create_by!(name: data["name"]) do |s|
|
||||||
s.country_id = country.id
|
s.country_id = country.id
|
||||||
|
Loading…
Reference in New Issue
Block a user