Given your clarification, I would use the first approach, but instead of just introducing an is_editor
boolean for Authorship
, you might want to the generalize the language and the concept and instead use ResourceContributorship
with a contributor_type
field which could now be either :author
or :editor
, but could be extended in the future.
Two has_many_through Relationships To Same Model
-
29-06-2022 - |
Question
I have a Contributor
Model and a Resource
Model. In a simple world I would have the following setup:
class Resource
has_many :authorships
has_many :contributors, through: :authorships
end
class Contributor
has_many :authorships
has_many :resources, through: :authorships
end
However, my requirements have changed. A contributor can now either be an editor of a resource or an author of a resource. A Contributor
can be an Editor
of one resource and the Author
of another. So it seems I have two ways to handle this requirement:
Add some kind of
is_editor?
attribute to myAuthorships
join model and effectively annotate each relationship.Create a second join model –
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
Which is the most sensible approach, or is there another approach I'm missing?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow