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.

On Tue, May 20, 2008 at 8:14 AM, John H. <
[email protected]> 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/.


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

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.

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

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 :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?

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

On Mon, May 19, 2008 at 7:40 PM, Ryan B. (Radar)
[email protected] 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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs