From 27100aec243dec466e7a490f0fc2db152088a098 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Mon, 10 Feb 2025 17:07:48 +0800 Subject: [PATCH] fix: improve country creation logic - Change method from find_or_create_by to find and check for nil - Initialize a new Country object only if none exists This fix addresses the potential issue where a country is being unnecessarily created instead of found, improving the efficiency of the country data synchronization process. This change also adds clarity to the method's intent by clearly separating the find and the initialization logic. --- lib/tasks/sync_geo_data.rake | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/tasks/sync_geo_data.rake b/lib/tasks/sync_geo_data.rake index 2e4490f..cb602f4 100644 --- a/lib/tasks/sync_geo_data.rake +++ b/lib/tasks/sync_geo_data.rake @@ -84,8 +84,9 @@ namespace :geo do count += 1 # 查找或初始化 Country - country = Country.find_or_create_by(code: data["iso2"]) do |c| - c.name = data["name"] + country = Country.find_by(code: data["iso2"]) + if country.nil? + country = Country.new end # 更新 Country 属性