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”(有许多公司)列表,但我想有两个这样的关联,但名称不同,因为它们是不同的字段。我该怎么做?我看到了一个新的答案。我想让你向我们描述一下你所说的“不同的名字”和“不同的领域”是什么意思。列出一个课程
模式的例子会有所帮助。是的!这就是我想要的。哈,对不起,是的!这就是我想要的。哈,对不起