Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 同一型号上有多个关系,多个字段名_Ruby On Rails_Ruby_Activerecord_Associations_Mongoid - Fatal编程技术网

Ruby on rails 同一型号上有多个关系,多个字段名

Ruby on rails 同一型号上有多个关系,多个字段名,ruby-on-rails,ruby,activerecord,associations,mongoid,Ruby On Rails,Ruby,Activerecord,Associations,Mongoid,我有一个型号公司,它有以下字段: field :label, type: String field :logo_url, type: String field :status, type: String belongs_to :company_lists, :class_name => 'Models::Persistence::CompanyList' 和CompanyList field :la

我有一个型号
公司
,它有以下字段:

  field :label,              type: String
  field :logo_url,           type: String
  field :status,             type: String
  belongs_to :company_lists, :class_name => 'Models::Persistence::CompanyList'
CompanyList

  field :label,            type: String
  field :status,           type: String
  has_and_belongs_to_many :companies, inverse_of: nil
我有
课程
模型,其中可能有两种类型的公司名单,即
招聘公司名单
赞助公司名单


课程
模型应如何具有相同的
公司列表上有许多
,但名称不同,以便每个列表ID可以不同(而不是别名)。另外,
课程
每个公司列表应该只有一种类型。

所以你想在
课程
上有两个
多个
通过不同的名称指向
公司
模型,对吗?那么为什么不直接使用一个
多个
关联呢

class CurriculumCompanyList < ActiveRecord::Base
  belongs_to :company
  belongs_to :curriculum
end

class Curriculum < ActiveRecord::Base
  has_many :curriculum_company_lists
  #here could be has_many as well if you point directly to companies instead of companylist
  has_one :hiring_company_list, through: :curriculum_company_lists
  has_one :sponsoring_company_list, through: :business_line_subscriptions
end

class CompanyList < ActiveRecord::Base
  has_many :curriculum_company_lists
  has_many :curriculums, through: :curriculum_company_lists
end

所以你想在
课程中有两个
多个
通过不同的名称指向
公司
模型,对吗?那么为什么不直接使用
多个关联呢

class CurriculumCompanyList < ActiveRecord::Base
  belongs_to :company
  belongs_to :curriculum
end

class Curriculum < ActiveRecord::Base
  has_many :curriculum_company_lists
  #here could be has_many as well if you point directly to companies instead of companylist
  has_one :hiring_company_list, through: :curriculum_company_lists
  has_one :sponsoring_company_list, through: :business_line_subscriptions
end

class CompanyList < ActiveRecord::Base
  has_many :curriculum_company_lists
  has_many :curriculums, through: :curriculum_company_lists
end

如果这些模型之间的关系不同,这表明您需要重新设计它。也许
公司列表
课程列表
可以是
列表
(从中继承)类型,并且在每个模型中定义唯一的关系。确定。有没有一种方法可以让我在同一型号上使用“has many”(有许多公司)但有不同的字段名?你能更具体一点吗?我想在我的
课程中使用“has many company”(有许多公司)列表,但我想有两个这样的关联,但名称不同,因为它们是不同的字段。我该怎么做?我看到了一个新的答案。我想让你向我们描述一下你所说的“不同的名称”和“不同的领域”是什么意思。列出一个
课程
模型的例子会有所帮助。如果这些模型之间的关系不同,这表明你需要重新设计它。也许
公司列表
课程列表
可以是
列表
(从中继承)类型,并且在每个模型中定义唯一的关系。确定。有没有一种方法可以让我在同一型号上使用“has many”(有许多公司)但有不同的字段名?你能更具体一点吗?我想在我的
课程中使用“has many company”(有许多公司)列表,但我想有两个这样的关联,但名称不同,因为它们是不同的字段。我该怎么做?我看到了一个新的答案。我想让你向我们描述一下你所说的“不同的名字”和“不同的领域”是什么意思。列出一个
课程
模式的例子会有所帮助。是的!这就是我想要的。哈,对不起,是的!这就是我想要的。哈,对不起