加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails与相同模型的关联

发布时间:2020-12-17 03:50:49 所属栏目:百科 来源:网络整理
导读:我有两个具有以下关联的类: class Incident has_one :assignee has_one :technicianclass User has_many :incidents 请注意,受理人和技术人员字段引用User类型的对象.这些关系应该如何在模型中? 解决方法 据推测,事件应属于受让人和技术人员,因为持有这些
我有两个具有以下关联的类:

class Incident
  has_one :assignee
  has_one :technician

class User 
  has_many :incidents

请注意,受理人和技术人员字段引用User类型的对象.这些关系应该如何在模型中?

解决方法

据推测,事件应属于受让人和技术人员,因为持有这些关系的外键将位于事件表中,而不是员工表中

class Incident
  belongs_to :assignee,:class_name => 'User'
  belongs_to :technician,:class_name => 'User'

class User 
  has_many :assigned_incidents,:class_name => 'Incident',:foreign_key => 'assignee_id'

  # not sure the wording you'd want to use for this relationship
  has_many :technician_incidents,:foreign_key => 'technician_id'

您希望外键字段为incidents.assignee_id,incidents.technician_id

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读