diff --git a/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb b/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb index 62f6bf073..7b6699426 100644 --- a/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb +++ b/backend/db/migrate/20231123093623_change_tourism_and_recreation_to_sustainable_tourism.rb @@ -3,16 +3,16 @@ def up Project.where(category: "tourism-and-recreation").update_all(category: "sustainable-tourism") [Investor, ProjectDeveloper].each do |model| - model.where("categories @> ?", "{tourism-and-recreation}").find_each do |record| + records_to_upsert = model.where("categories @> ?", "{tourism-and-recreation}") + .map do |record| new_categories = record.categories.map do |category| category == "tourism-and-recreation" ? "sustainable-tourism" : category end - data = record.attributes - data['categories'] = new_categories - - model.upsert_all([data], unique_by: :id) + record.attributes.merge(categories: new_categories) end + + model.upsert_all(records_to_upsert, unique_by: :id) unless records_to_upsert.empty? end end @@ -20,16 +20,16 @@ def down Project.where(category: "sustainable-tourism").update_all(category: "tourism-and-recreation") [Investor, ProjectDeveloper].each do |model| - model.where("categories @> ?", "{sustainable-tourism}").find_each do |record| + records_to_upsert = model.where("categories @> ?", "{sustainable-tourism}") + .map do |record| new_categories = record.categories.map do |category| category == "sustainable-tourism" ? "tourism-and-recreation" : category end - data = record.attributes - data['categories'] = new_categories - - model.upsert_all([data], unique_by: :id) + record.attributes.merge(categories: new_categories) end + + model.upsert_all(records_to_upsert, unique_by: :id) unless records_to_upsert.empty? end end end