diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index 4daa0aae3..783c48ca7 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -30,4 +30,10 @@ class RepositoryRow < ApplicationRecord def self.name_like(query) where('repository_rows.name ILIKE ?', "%#{query}%") end + + def self.change_owner(team, user, new_owner) + joins(:repository) + .where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user) + .update_all(created_by_id: new_owner.id) + end end diff --git a/app/models/user_team.rb b/app/models/user_team.rb index f6d703f48..567b4d594 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -79,12 +79,7 @@ class UserTeam < ApplicationRecord # Make new owner author of all inventory items that were added # by departing user and belong to this team. - repository_rows = - RepositoryRow.joins(:repository) - .where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user) - repository_rows.find_each do |repository_row| - repository_row.update(created_by: new_owner) - end + RepositoryRow.change_owner(team, user, new_owner) super() end