Here is a short working example on how you can create polymorphic has_many :through relationships in Rails 3.
Imagine you have an Author model. Each Author can write either Books or Articles (or other things). Each author can have a multitude of publications, and each publication can have multiple authors.
We want to be flexible on the kind and number of publications we model, and want that side of the many-to-many relationship to be polymorphic. Clearly we will need a join model between the publications and the authors. We want to store more information in that join model, e.g. the publication year for now. We will call the join model Authorships, and use has_many :through to reach through to the other model on each side of the join table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
This allows you to easily access the other side of each relationship like this:
a = Author.first a.books a.articles b = Book.first b.authors a = Article.first a.authors
Having set up the relations like this, allows us to easily extend this to have authors write other things, e.g. blogposts or comments. All we need to do is to create classes for those models similar to "Books" and "Articles" in the above example, and to extend the relations of "Author" to point to the blogposts or comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Gist for Polymorphic Rails 3 has_many :through Relationship