2014-01-09 46 views
0

会员人和公司通过就业和通过面试是不好的做法? 为什么,具体? 这个答案是否适用于一般的数据库,而不仅仅是rails?Rails - 使用多重has_many通过:关联加入相同的两个模型。冗余has_many通过:

实施例:

employment.rb

class Employment < ActiveRecord::Base 
    belongs_to :people 
    belongs_to :companies 
end 

interview.rb

class Interview < ActiveRecord::Base 
    belongs_to :people 
    belongs_to :companies 
end 

person.rb

class Person < ActiveRecord::Base 
    has_many :employments 
    has_many :interviews 
    has_many :companies, through: :employments 
    has_many :companies, through: :interviews 
end 

company.rb

class Company < ActiveRecord::Base 
    has_many :employments 
    has_many :interviews 
    has_many :companies, through: :employments 
    has_many :companies, through: :interviews 
end 

人和公司通过就业相关联,但也通过面试冗余。

回答

1

让两个模型有多个关联(包括同一类型的多个关联)没有任何问题。但是,您可能希望为这些关联提供唯一的名称 - 否则,当您拨打(例如)@person.companies时,您不知道自己会得到什么 - 公司通过招聘或公司通过面试。

我相信这个问题有一个体面的例子:ruby on rails has_many :through association which has two columns with same model

+0

你的“加盟”模式的一个意思,给了主力车型之一化名? – ahnbizcad

+0

@ gwho5150我的意思是'主要'模型 - 重要的是当您引用相关模型的(集合)时避免歧义。 – MrTheWalrus