# Polymorphic association now needs many to many relationship

**URL:** <https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074>\
**Category:** Rails\
**Created:** [May 20, 2008, 12:44am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074 "2008-05-20T00:44:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 12:44am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/1 "2008-05-20T00:44:42Z")

</div>

I have a polymorphic relationship between reviews and books/articles.  
It is working well under the assumption that each review is only related  
to a single book or article. Now, I realize that reviews can be  
associated with many books or articles. I am trying to figure out the  
best way to migrate given that I already have this in production.

class Review \< ActiveRecord::Base  
belongs\_to :reviewed, :polymorphic =\> true  
end

class Article \< ActiveRecord::Base  
has\_many :reviews, :as =\> :reviewed  
end

class Book \< ActiveRecord::Base  
has\_many :reviews, :as =\> :reviewed  
end

This is the current relevant code the basic polymorphic association.  
Any ideas on how I can make reviews be applicable to multiple books or  
articles. I want to be able to do things like review.reviewed \<\<  
[book1, article 1, book2]

Thanks.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 1:41am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/2 "2008-05-20T01:41:10Z")

</div>

Wouldn’t a review only be relevant to one article or book at any time?

I can’t review King Kong and pass it off also as a review to Iron Man.

On Tue, May 20, 2008 at 8:14 AM, John H. \<  
removed\_email\_address@domain.invalid\> wrote:

> articles. I want to be able to do things like review.reviewed \<\<  
> [book1, article 1, book2]
> 
> ## Thanks.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Appreciated my help?  
Reccommend me on Working With Rails  
[http://workingwithrails.com/person/11030-ryan-bigg](http://workingwithrails.com/person/11030-ryan-bigg)

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 1:52am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/3 "2008-05-20T01:52:29Z")

</div>

Ryan B. wrote:

> Wouldn’t a review only be relevant to one article or book at any time?

In the reviews I am using, the review could mention or cover multiple  
articles or books. Let’s say I am writing a review on Indiana Jones, I  
many want to associate it with multiple sequels, etc.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 3:37am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/4 "2008-05-20T03:37:01Z")

</div>

- John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2008-05-20 00:44:42  
+0200]:

> articles. I want to be able to do things like review.reviewed \<\<  
> [book1, article 1, book2]

Hi,

I think you can use has\_many :through to build the m2m relationship, and  
use its :source and :source\_type options.

Best,  
Jan

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 4:07am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/5 "2008-05-20T04:07:02Z")

</div>

John H. wrote:

> Ryan B. wrote:
> 
> > Wouldn’t a review only be relevant to one article or book at any time?
> 
> In the reviews I am using, the review could mention or cover multiple  
> articles or books. Let’s say I am writing a review on Indiana Jones, I  
> many want to associate it with multiple sequels, etc.

Why not add a “related” model to the mix to handle this?

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 4:08am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/6 "2008-05-20T04:08:45Z")

</div>

Andrew S. wrote:

> John H. wrote:
> 
> > Ryan B. wrote:
> > 
> > > Wouldn’t a review only be relevant to one article or book at any time?
> > 
> > In the reviews I am using, the review could mention or cover multiple  
> > articles or books. Let’s say I am writing a review on Indiana Jones, I  
> > many want to associate it with multiple sequels, etc.
> 
> Why not add a “related” model to the mix to handle this?

Andrew,

Can you expand on how this related model will work?

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 20, 2008, 4:31am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/7 "2008-05-20T04:31:17Z")

</div>

John H. wrote:

> Andrew S. wrote:
> 
> > John H. wrote:
> > 
> > > Ryan B. wrote:
> > > 
> > > > Wouldn’t a review only be relevant to one article or book at any time?
> > > 
> > > In the reviews I am using, the review could mention or cover multiple  
> > > articles or books. Let’s say I am writing a review on Indiana Jones, I  
> > > many want to associate it with multiple sequels, etc.
> > 
> > Why not add a “related” model to the mix to handle this?
> 
> Andrew,
> 
> Can you expand on how this related model will work?

Sure - something like the following:

class Review \< ActiveRecord::Base  
belongs\_to :Book  
has\_many :relationships, :as =\> :relation  
end

class Article \< ActiveRecord::Base  
has\_many :reviews  
has\_many :relationships, :as =\> :relation  
end

class Book \< ActiveRecord::Base  
has\_many :reviews  
has\_many :relationships, :as =\> :relation  
end

class Related \< Active::Base  
belongs\_to :relation, polymorphic =\> true  
end

Note: I am distracted by work here, so this may not be exactly what  
your looking for 🙂

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 21, 2008, 10:13am UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/8 "2008-05-21T10:13:35Z")

</div>

has\_many\_polymorphs is the plugin you need.

class Review \< ActiveRecord::Base  
has\_many\_polymorphs :links, :from =\> [:books, :articles]  
end

class LinksReview \< ActiveRecord::Base  
belongs\_to :review  
belongs\_to :link, :polymorphic =\> true, :depend =\> :destroy  
end

However, does anyone have a performance benchmarks on this plugin?

On Tue, May 20, 2008 at 6:44 AM, John H. \<

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [May 21, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/polymorphic-association-now-needs-many-to-many-relationship/138074/9 "2008-05-21T14:19:50Z")

</div>

On Mon, May 19, 2008 at 7:40 PM, Ryan B. (Radar)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Wouldn’t a review only be relevant to one article or book at any time?
> 
> I can’t review King Kong and pass it off also as a review to Iron Man.

Not necessarily, I suspect he’s interested in modeling a review which  
might compare Iron Man to King Kong and therefore in effect review  
both.

## Look at things like the New York Times Book Review section and you’ll see that such parallel reviews aren’t uncommon at all.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
