【问题标题】:Rails - Using multple has_many through: associations joining same two models. Redundant has_many through:Rails - 通过使用多个 has_many:关联加入相同的两个模型。冗余has_many通过:
【发布时间】:2014-01-09 22:04:38
【问题描述】:

通过雇佣和面试的方式联系个人和公司是不好的做法吗? 为什么,具体来说? 这个答案是否适用于一般的数据库,而不仅仅是 Rails?

例子:

就业.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

公司.rb

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

个人和公司通过雇佣联系起来,但也通过面试冗余。

【问题讨论】:

    标签: sql ruby-on-rails database associations


    【解决方案1】:

    让两个模型具有多个关联(包括同一类型的多个关联)并没有错。不过,您会希望给协会起唯一的名称 - 否则当您致电(例如)@person.companies 时,您将不知道您将获得什么 - 通过就业获得的公司,或通过面试获得的公司。

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

    【讨论】:

    • 你的意思是在“加入”模型之一,为其中一个主要模型提供化名?
    • @gwho5150 我的意思是“主要”模型——重要的是避免在引用相关模型(集合)时产生歧义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    相关资源
    最近更新 更多