2013-09-26 55 views
0

我有一个Contributor模型和一个Resource模型。在一个简单的世界里,我会有以下设置:两个has_many_through关系到相同模型

class Resource 

    has_many :authorships 
    has_many :contributors, through: :authorships 

end 

class Contributor 

    has_many :authorships 
    has_many :resources, through: :authorships 

end 

但是,我的要求已经改变。贡献者现在可以是资源的编辑者或资源的作者。 A Contributor可以是一个资源的Editor和另一资源的Author。如此看来,我有两个方法来处理这个要求:

  1. 添加某种is_editor?属性我Authorships加盟模式,有效地标注每个关系。

  2. 创建第二个连接模型 - Editorship

    class Resource 
        has_many :authorships 
        has_many :editorships 
        has_many :contributors, through: :authorships 
        has_many :contributors, through: :editorships 
    
    end 
    
    class Contributor 
        has_many :authorships 
        has_many :editorships 
        has_many :resources, through: :authorships 
        has_many :resources, through: :editorships 
    end 
    

这是最明智的做法,还是有另一种方法,我失踪?

+0

Rails多态协会支持第一种情况,但我认为第一个问题是您需要/想要支持同时是作者和编辑者的“Contributor”吗? –

+0

“编辑”和“作者”都是相同的。它们在各个方面都可以互换。我不想子类化并建立多态关系,因为这感觉不对。在我看来,Join更清楚地描述了这种关系。由于相同的数据既可以作为作者也可以作为编辑,我最终也会有很多重复。 – Undistraction

+0

我的问题是,一个人是否可以成为一名编辑,并且是同一个资源的作者。 –

回答

1

鉴于你澄清,我会用第一种方法,但不是仅仅引入is_editor布尔为Authorship,你可能想的概括的语言和概念,而是使用ResourceContributorshipcontributor_type场现在可以或者:author:editor,但可以在将来进行扩展。

+0

感谢您的回答。这是有道理的。 – Undistraction

相关问题