FIX: Delete votes associated with no user (#85)

FIX: Delete votes associated with no user (#85)

Co-authored-by: RĂ©gis Hanol regis@hanol.fr

diff --git a/app/jobs/onceoff/voting_ensure_consistency.rb b/app/jobs/onceoff/voting_ensure_consistency.rb
index 5f4c319..13165ba 100644
--- a/app/jobs/onceoff/voting_ensure_consistency.rb
+++ b/app/jobs/onceoff/voting_ensure_consistency.rb
@@ -35,6 +35,12 @@ module Jobs
               dvv1.archive = dvv2.archive
       SQL
 
+      # delete votes associated with no user
+      DB.exec(<<~SQL)
+        DELETE FROM discourse_voting_votes
+        WHERE user_id NOT IN (SELECT id FROM users)
+      SQL
+
       # delete votes associated with no topics
       DB.exec(<<~SQL)
         DELETE FROM discourse_voting_votes
diff --git a/plugin.rb b/plugin.rb
index a00aaf1..34829b1 100755
--- a/plugin.rb
+++ b/plugin.rb
@@ -301,6 +301,8 @@ after_initialize do
 
     if orig.who_voted.present? && orig.closed
       orig.who_voted.each do |user|
+        next if user.blank?
+
         user_votes = user.topics_with_vote.pluck(:topic_id)
         user_archived_votes = user.topics_with_archived_vote.pluck(:topic_id)
 

GitHub sha: 9e02c59b

This commit appears in #85 which was approved by ZogStriP. It was merged by udan11.