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);
This commit is contained in:
Absurdon 2019-08-24 20:17:14 +02:00
parent 91a8cdca59
commit e93588591b
No known key found for this signature in database
GPG key ID: E383BEAE6F394B2F
2 changed files with 4 additions and 1 deletions

View file

@ -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]

View file

@ -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 >= ?)",