Tom Salporia
2008-Jan-18 16:33 UTC
has_many :through with self-referencial AND polymorphism
Hello, despite many readings I still don''t understand how to solve my problem. I have Publication, Picture, File classes A Publication can be related (a one way relation, like a www link) with another Publication, Picture or File I tried this : Publication : has_many :related_elements, :dependent => :destroy, :foreign_key => :publication_id has_many :related, :through => :related_elements, :as => :linked, :source => :publication RelatedElement : belongs_to :publication And migrate as create_table :related_elements do |t| t.integer :linked_id t.string :linked_type t.integer :publication_id end When I do a_publication.related[0] = another_publication a_publication.save! I would expect the related_elements table beeing updated with publication_id = the id of a_publication linked_id = the id of another_publication linked_type = the class name ''Publication'' But nothing happen... And I must confess that I''m pretty sure my has_many are totally wrong All this stuff is really too complex for me, so your help will be greatly appreciated !! 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 -~----------~----~----~----~------~----~------~--~---
Pedro Del Gallego
2008-Jan-18 22:45 UTC
Re: has_many :through with self-referencial AND polymorphism
here you have an example http://pastie.org/140772 I hope it helps. 2008/1/18, Tom Salporia <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Hello, despite many readings I still don''t understand how to solve my > problem. > > I have Publication, Picture, File classes > A Publication can be related (a one way relation, like a www link) with > another Publication, Picture or File > > I tried this : > > Publication : > has_many :related_elements, :dependent => :destroy, :foreign_key => > :publication_id > has_many :related, :through => :related_elements, :as => :linked, > :source => :publication > > RelatedElement : > belongs_to :publication > > And migrate as > create_table :related_elements do |t| > t.integer :linked_id > t.string :linked_type > t.integer :publication_id > end > > When I do > a_publication.related[0] = another_publication > a_publication.save! > > I would expect the related_elements table beeing updated with > publication_id = the id of a_publication > linked_id = the id of another_publication > linked_type = the class name ''Publication'' > > But nothing happen... And I must confess that I''m pretty sure my > has_many are totally wrong > > All this stuff is really too complex for me, so your help will be > greatly appreciated !! Thanks > -- > Posted via http://www.ruby-forum.com/. > > > >-- ------------------------------------- Pedro Del Gallego Email : pedro.delgallego-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tom Salporia
2008-Jan-21 16:34 UTC
Re: has_many :through with self-referencial AND polymorphism
Pedro Del gallego wrote: Thanks Pedro, this is a first step in the right direction. Now I would like to add support for polymorphism, I mean that the item related could be of different class (this -> related_to -> book, this -> related_to -> image, and so on) It seems that it''s not a simple case (http://blog.hasmanythrough.com/2006/4/3/polymorphic-through) Does it changed with Rails 2.0 ? I tried the solution provided on the hasmanythrough blog but without success. Should I go to the has_many_polymorphs plugin ?? (http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs/files/README.html) I don''t like the idea of overusing plugins when I don''t know how to handle a special situation... It''s kind of black box in that case 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 -~----------~----~----~----~------~----~------~--~---