From 90c491637b65971f067a1f43b47ccd621a851038 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Tue, 11 Feb 2025 10:50:08 +0800 Subject: [PATCH] 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 --- lib/tasks/sync_geo_data.rake | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/tasks/sync_geo_data.rake b/lib/tasks/sync_geo_data.rake index d59510f..5750bed 100644 --- a/lib/tasks/sync_geo_data.rake +++ b/lib/tasks/sync_geo_data.rake @@ -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