Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 两个相同的模型属于关联_Ruby On Rails 3_Associations_Model Associations - Fatal编程技术网

Ruby on rails 3 两个相同的模型属于关联

Ruby on rails 3 两个相同的模型属于关联,ruby-on-rails-3,associations,model-associations,Ruby On Rails 3,Associations,Model Associations,我有一个模型接触点,它有许多系统。从系统方面,我想将接触点确定为技术经理或项目经理(或两者兼而有之)。但仍然只在数据库中保留接触点1次 我的尝试如下: class System < ActiveRecord::Base belongs_to :project_manager, :class_name => 'PointOfContact' belongs_to :technical_manager, :class_name => 'PointOfContact' end

我有一个模型
接触点
,它有许多
系统
。从
系统
方面,我想将
接触点
确定为
技术经理
项目经理
(或两者兼而有之)。但仍然只在数据库中保留
接触点
1次

我的尝试如下:

class System < ActiveRecord::Base
  belongs_to :project_manager, :class_name => 'PointOfContact'
  belongs_to :technical_manager, :class_name => 'PointOfContact'
end

class PointOfContact < ActiveRecord::Base
  has_many :systems
end

多亏了RailsForum.com上的jamesw:找到了解决方案

class System < ActiveRecord::Base
  belongs_to :project_manager, :class_name => 'PointOfContact', :foreign_key => 'project_manager_id'
  belongs_to :technical_manager, :class_name => 'PointOfContact', :foreign_key => 'technical_manager_id'
end

class PointOfContact < ActiveRecord::Base
  has_many :project_managed_systems, :class_name => 'System', :foreign_key => 'project_manager_id'
  has_many :technical_managed_systems, :class_name => 'System', :foreign_key => 'technical_manager_id'
end
类系统'PointOfContact',:外键=>'project\u manager\u id'
属于:技术经理,:class\u name=>'PointOfContact',:foreign\u key=>'technical\u manager\u id'
结束
类PointOfContact'System',:外键=>'project\u manager\u id'
有很多:技术管理系统,:类名称=>'System',:外键=>'technical\u manager\u id'
结束

来自Rails文档:

注释示例:

# Employee class with two Employee associations
class Employee < ApplicationRecord

  # Employees I manage
  has_many :subordinates, class_name: "Employee",
                          foreign_key: "manager_id"

  # Employee that manages me
  # NOTE: with :manager reference name, foreign_key defaults to "manager_id",
  # hence it is not needed as above. Favor "convention over configuration".
  belongs_to :manager, class_name: "Employee"
end
#具有两个员工关联的员工类
类员工<应用程序记录
#我管理的员工
有很多:下属,班名:“员工”,
外键:“经理id”
#管理我的员工
#注意:使用:manager reference name,外键默认为“manager\u id”,
#因此,不需要如上所述。赞成“约定胜于配置”。
属于:经理,班名:“员工”
结束

有关的更多详细信息:通过上的关系
# Employee class with two Employee associations
class Employee < ApplicationRecord

  # Employees I manage
  has_many :subordinates, class_name: "Employee",
                          foreign_key: "manager_id"

  # Employee that manages me
  # NOTE: with :manager reference name, foreign_key defaults to "manager_id",
  # hence it is not needed as above. Favor "convention over configuration".
  belongs_to :manager, class_name: "Employee"
end