polymorphic association now needs many to many relationship

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.

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.

Ryan Bigg 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.

* John Honovich <rails-mailing-list@andreas-s.net> [2008-05-20 00:44:42 +0200]:

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]

Hi,

I think you can use has_many :through to build the m2m relationship, and use its :source and :source_type options.

Best, Jan

John Honovich wrote:

Ryan Bigg 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 Skegg wrote:

John Honovich wrote:

Ryan Bigg 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?

John Honovich wrote:

Andrew Skegg wrote:

John Honovich wrote:

Ryan Bigg 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 :slight_smile:

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?

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.