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