两对象同时映射一对一和一对多_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > 两对象同时映射一对一和一对多

两对象同时映射一对一和一对多

 2019/2/20 13:33:04  scholltop  程序员俱乐部  我要评论(0)
  • 摘要:classKpi::Team<ApplicationRecordhas_many:crm_teamshas_many:business_managers,through::crm_teams,source::business_managerhas_many:account_managers,through::crm_teams,source::account_managerhas_many:purchaser_managers,through::crm_teams,source:
  • 标签:
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
上一篇: C# 操作Word目录——生成、删除目录 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名