From 3cee4149b1232b527ae72a6cf8079a6a5a321ee6 Mon Sep 17 00:00:00 2001 From: Monica Cellio Date: Fri, 23 Aug 2024 10:53:31 -0400 Subject: [PATCH 1/2] don't try to update rep for the nil user --- app/models/post.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index aecc13bd3..7ceb712f4 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -260,10 +260,12 @@ def modify_author_reputation sc = saved_changes if sc.include?('deleted') && sc['deleted'][0] != sc['deleted'][1] && created_at >= 60.days.ago deleted = !!saved_changes['deleted']&.last - if deleted - user.update(reputation: user.reputation - Vote.total_rep_change(votes)) - else - user.update(reputation: user.reputation + Vote.total_rep_change(votes)) + if user + if deleted + user.update(reputation: user.reputation - Vote.total_rep_change(votes)) + else + user.update(reputation: user.reputation + Vote.total_rep_change(votes)) + end end end end From 60903e149dbfb9d27d1aaf907af1a3de02544f1c Mon Sep 17 00:00:00 2001 From: Monica Cellio Date: Sat, 24 Aug 2024 22:26:21 -0400 Subject: [PATCH 2/2] PR review Co-authored-by: ArtOfCode --- app/models/post.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 7ceb712f4..de81fb53e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -260,12 +260,10 @@ def modify_author_reputation sc = saved_changes if sc.include?('deleted') && sc['deleted'][0] != sc['deleted'][1] && created_at >= 60.days.ago deleted = !!saved_changes['deleted']&.last - if user - if deleted - user.update(reputation: user.reputation - Vote.total_rep_change(votes)) - else - user.update(reputation: user.reputation + Vote.total_rep_change(votes)) - end + if deleted + user&.update(reputation: (user&.reputation || 1) - Vote.total_rep_change(votes)) + else + user&.update(reputation: (user&.reputation || 1) + Vote.total_rep_change(votes)) end end end