From 04ce935e691eda640938bfa87952f7780e402682 Mon Sep 17 00:00:00 2001 From: Echo Date: Mon, 1 Dec 2025 11:12:21 -0500 Subject: [PATCH] fix n+1 on mailing job (#673) --- app/jobs/check_streak_physical_mail_job.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/jobs/check_streak_physical_mail_job.rb b/app/jobs/check_streak_physical_mail_job.rb index 7ed7d24..591e84d 100644 --- a/app/jobs/check_streak_physical_mail_job.rb +++ b/app/jobs/check_streak_physical_mail_job.rb @@ -14,8 +14,14 @@ class CheckStreakPhysicalMailJob < ApplicationJob over_7_day_streaks = streaks.select { |_, streak| streak > 7 }.keys + existing_user_ids = PhysicalMail.going_out + .where(mission_type: :first_time_7_streak) + .where(user_id: over_7_day_streaks) + .pluck(:user_id) + .to_set + over_7_day_streaks.each do |user_id| - next if PhysicalMail.going_out.exists?(user_id: user_id, mission_type: :first_time_7_streak) + next if existing_user_ids.include?(user_id) user = User.find(user_id)