diff --git a/app/models/badge.rb b/app/models/badge.rb index de1b82139..38011348a 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -378,7 +378,7 @@ SQL DELETE FROM user_badges USING user_badges ub LEFT JOIN users u ON u.id = ub.user_id - WHERE u.id IS NULL + WHERE u.id IS NULL AND user_badges.id = ub.id SQL Badge.find_each(&:reset_grant_count!) diff --git a/spec/models/badge_spec.rb b/spec/models/badge_spec.rb index 59e997b66..3ee6bf8ce 100644 --- a/spec/models/badge_spec.rb +++ b/spec/models/badge_spec.rb @@ -49,11 +49,12 @@ describe Badge do b.save UserBadge.create!(user_id: -100, badge_id: b.id, granted_at: 1.minute.ago, granted_by_id: -1) + UserBadge.create!(user_id: User.first.id, badge_id: b.id, granted_at: 1.minute.ago, granted_by_id: -1) Badge.ensure_consistency! b.reload - expect(b.grant_count).to eq(0) + expect(b.grant_count).to eq(1) end end