From 9d03c3ce7915aeb2933dded2fd7be64d0ab88b66 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 2 May 2024 12:24:30 +0100 Subject: [PATCH] Update records in batches (#2400) --- lib/tasks/update_created_by_values.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/update_created_by_values.rake b/lib/tasks/update_created_by_values.rake index cfdd2853b..572d88e63 100644 --- a/lib/tasks/update_created_by_values.rake +++ b/lib/tasks/update_created_by_values.rake @@ -1,14 +1,14 @@ desc "Updates created_by values for lettings and sales logs" task update_created_by_values: :environment do LettingsLog.filter_by_years(%w[2023 2024]).where.not(bulk_upload_id: nil).update_all("created_by_id = (SELECT user_id FROM bulk_uploads WHERE bulk_uploads.id = lettings_logs.bulk_upload_id)") - LettingsLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil).each do |lettings_log| + LettingsLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil, created_by: nil).find_each do |lettings_log| user = PaperTrail::Version.find_by(item_id: lettings_log.id, item_type: "LettingsLog", event: "create")&.actor lettings_log.created_by = user.presence || lettings_log.assigned_to lettings_log.save!(touch: false, validate: false) end SalesLog.filter_by_years(%w[2023 2024]).where.not(bulk_upload_id: nil).update_all("created_by_id = (SELECT user_id FROM bulk_uploads WHERE bulk_uploads.id = sales_logs.bulk_upload_id)") - SalesLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil).each do |sales_log| + SalesLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil, created_by: nil).find_each do |sales_log| user = PaperTrail::Version.find_by(item_id: sales_log.id, item_type: "SalesLog", event: "create")&.actor sales_log.created_by = user.presence || sales_log.assigned_to sales_log.save!(touch: false, validate: false)