From e93588591b5652e4a3536c56e5b1b485e5c0982c Mon Sep 17 00:00:00 2001 From: Absurdon Date: Sat, 24 Aug 2019 20:17:14 +0200 Subject: [PATCH] Fix #131 Updating users may fail Issue: When a team gets deleted team_id is not properly cleared from users who have set the team to be deleted as their main. When now someone trys an operation that validates the user it fails as the team_id set on that user is not valid anymore. Solution: To prevent this from happening in the future update users and set team_id to nil. Clean up: To clean up existing users run UPDATE users SET team_id=NULL WHERE team_id NOT IN(SELECT id FROM teams); --- app/models/team.rb | 3 +++ app/models/teamer.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/team.rb b/app/models/team.rb index 8715593..5c6d539 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -93,6 +93,9 @@ class Team < ActiveRecord::Base end def destroy + User.where(team_id: self.id).each do |user| + user.update_attribute(:team_id, nil) + end if matches.count > 0 update_attribute :active, false teamers.update_all ["rank = ?", Teamer::RANK_REMOVED] diff --git a/app/models/teamer.rb b/app/models/teamer.rb index 54361ca..07c4153 100644 --- a/app/models/teamer.rb +++ b/app/models/teamer.rb @@ -50,7 +50,7 @@ class Teamer < ActiveRecord::Base scope :distinct, :group => "user_id, team_id" scope :ordered, - :order => "rank DESC, created_at ASC" + :order => "`rank` DESC, `created_at` ASC" scope :historic, lambda { |user, time| {:conditions => ["user_id = ? AND created_at < ? AND ((updated_at > ? AND rank = ?) OR rank >= ?)",