John Honovich
2008-May-19 22:44 UTC
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-May-19 23:40 UTC
Re: polymorphic association now needs many to many relationship
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 Honovich < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Appreciated my help? Reccommend me on Working With Rails http://workingwithrails.com/person/11030-ryan-bigg --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Honovich
2008-May-19 23:52 UTC
Re: polymorphic association now needs many to many relations
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xie Hanjian
2008-May-20 01:34 UTC
Re: polymorphic association now needs many to many relationship
* John Honovich <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> [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> > Thanks. > -- > Posted via http://www.ruby-forum.com/. > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~--- >-- jan=callcc{|jan|jan};jan.call(jan)
Andrew Skegg
2008-May-20 02:07 UTC
Re: polymorphic association now needs many to many relations
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Honovich
2008-May-20 02:08 UTC
Re: polymorphic association now needs many to many relations
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Skegg
2008-May-20 02:31 UTC
Re: polymorphic association now needs many to many relations
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 :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
sishen
2008-May-21 08:13 UTC
Re: polymorphic association now needs many to many relationship
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 Honovich < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick DeNatale
2008-May-21 12:19 UTC
Re: polymorphic association now needs many to many relationship
On Mon, May 19, 2008 at 7:40 PM, Ryan Bigg (Radar) <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---