我有一个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
。如此看来,我有两个方法来处理这个要求:
添加某种
is_editor?
属性我Authorships
加盟模式,有效地标注每个关系。创建第二个连接模型 -
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
这是最明智的做法,还是有另一种方法,我失踪?
Rails多态协会支持第一种情况,但我认为第一个问题是您需要/想要支持同时是作者和编辑者的“Contributor”吗? –
“编辑”和“作者”都是相同的。它们在各个方面都可以互换。我不想子类化并建立多态关系,因为这感觉不对。在我看来,Join更清楚地描述了这种关系。由于相同的数据既可以作为作者也可以作为编辑,我最终也会有很多重复。 – Undistraction
我的问题是,一个人是否可以成为一名编辑,并且是同一个资源的作者。 –