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.
This commit is contained in:
songtianlun 2025-02-10 17:07:48 +08:00
parent 3f1e8892f9
commit 27100aec24

View File

@ -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 属性