class="ruby" name="code"> class Kpi::Team < ApplicationRecord has_many :crm_teams has_many :business_managers, through: :crm_teams, source: :business_manager has_many :account_managers, through: :crm_teams, source: :account_manager has_many :purchaser_managers, through: :crm_teams, source: :purchaser_manager has_many :customer_managers, through: :crm_teams, source: :customer_manager belongs_to :business_manager, class_name: :Manager, foreign_key: :business_manager_id belongs_to :account_manager, class_name: :Manager, foreign_key: :account_manager_id belongs_to :purchaser_manager, class_name: :Manager, foreign_key: :purchaser_manager_id belongs_to :customer_manager, class_name: :Manager, foreign_key: :customer_manager_id after_save :change_managers def change_managers if self.saved_change_to_business_manager_id? Kpi::CrmTeam.create(team_id:self.id,manager_type:"BusinessManager",manager_id:self.business_manager_id) self.business_manager.update(team_id:[self.id]) end if self.saved_change_to_account_manager_id? Kpi::CrmTeam.create(team_id:self.id,manager_type:"AccountManager",manager_id:self.account_manager_id) self.account_manager.update(team_id:[self.id]) end if self.saved_change_to_purchaser_manager_id? Kpi::CrmTeam.create(team_id:self.id,manager_type:"PurchaserManager",manager_id:self.purchaser_manager_id) self.purchaser_manager.update(team_id:(self.purchaser_manager.team_id+[self.id]).uniq) end if self.saved_change_to_customer_manager_id? Kpi::CrmTeam.create(team_id:self.id,manager_type:"CustomerManager",manager_id:self.customer_manager_id) self.customer_manager.update(team_id:(self.customer_manager.team_id+[self.id]).uniq) end end end ————————————————————— class Kpi::CrmTeam < ApplicationRecord # 小组角色多肽 # 记录历史组员 belongs_to :team, class_name: 'Kpi::Team', optional: true belongs_to :manager belongs_to :business_manager, foreign_key: :manager_id, optional: true belongs_to :account_manager, foreign_key: :manager_id, optional: true belongs_to :purchaser_manager, foreign_key: :manager_id, optional: true belongs_to :customer_manager, foreign_key: :manager_id, optional: true end