2013-09-26 96 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

相關問題