Theo Graham-brown
2008-Feb-04 19:15 UTC
Need help with self-referencing models and relationships
Okay, I have the site running (http://www.londonprogressivejournal.com) and if you look you can ''respond'' to any given article which leads to you submitting a ''letter'' object to the system. At a later date we''ll be publishing these. Articles can of course have many letters. Letters only have one article. However, people will be able to respond to a published letter just as they will to an article. So a Letter can have many letters (those that are responses to it)...but it also only has one letter if it is itself a response to an existing letter. My Letter model has both an article_id and a letter_id but my issue is how to show Ruby that model? I have said ''has many letters'' but now of course I can''t refer to letter.letter.id to find out which letter this one might be a response to. Does anyone know how to deal with this sort of self-referential architecture in ruby? Cheers Theo -- 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 -~----------~----~----~----~------~----~------~--~---
You can define these relationships yourself using something like this: class Letter < ActiveRecord::Base belongs_to :article belongs_to :parent, :foreign_key => ''parent_id'', :class_name => ''Letter'' has_many :children, :foreign_key => ''parent_id, :class_name => ''Letter'', :dependent => :destroy end But I would recommend taking a look at the acts_as_tree and acts_as_nested_set plugins and see if they might provide what you are looking for. acts_as_tree provides the parent and children associations like I listed above. acts_as_nested set allows you to retrieve all children in a single query. (http://dev.rubyonrails.com/ svn/rails/plugins/) Aaron On Feb 4, 12:15 pm, Theo Graham-brown <rails-mailing-l...@andreas- s.net> wrote:> Okay, > > I have the site running (http://www.londonprogressivejournal.com) and if > you look you can ''respond'' to any given article which leads to you > submitting a ''letter'' object to the system. > > At a later date we''ll be publishing these. > > Articles can of course have many letters. Letters only have one article. > > However, people will be able to respond to a published letter just as > they will to an article. > > So a Letter can have many letters (those that are responses to it)...but > it also only has one letter if it is itself a response to an existing > letter. > > My Letter model has both an article_id and a letter_id but my issue is > how to show Ruby that model? I have said ''has many letters'' but now of > course I can''t refer to letter.letter.id to find out which letter this > one might be a response to. > > Does anyone know how to deal with this sort of self-referential > architecture in ruby? > > Cheers > Theo > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Starr Trader
2008-Feb-04 23:37 UTC
Re: Need help with self-referencing models and relationships
Theo Graham-brown wrote:> Okay, > > I have the site running (http://www.londonprogressivejournal.com) and if > you look you can ''respond'' to any given article which leads to you > submitting a ''letter'' object to the system. > > At a later date we''ll be publishing these. > > Articles can of course have many letters. Letters only have one article. > > However, people will be able to respond to a published letter just as > they will to an article. > > So a Letter can have many letters (those that are responses to it)...but > it also only has one letter if it is itself a response to an existing > letter. > > My Letter model has both an article_id and a letter_id but my issue is > how to show Ruby that model? I have said ''has many letters'' but now of > course I can''t refer to letter.letter.id to find out which letter this > one might be a response to. > > Does anyone know how to deal with this sort of self-referential > architecture in ruby? > > Cheers > TheoLet me restate your problem from the Letter''s point of view. A Letter belongs_to a parent (either a Article or another Letter). It also has_many responses (always other letters). I would build you letter class like this. class Letter < ActiveRecord::Base belongs_to :parent, :polymorphic=>true has_many :responses, :class_name =>''Letter'', :as=>:parent end Same thing for Article class Article < ActiveRecord::Base has_many :letters, :as=>:parent end As a side benefit, you can attach a letter to anything else on your site. You may also want to add validation on the belongs_to association to ensure that it only contains the classes you wish to attach letters to. -- 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 -~----------~----~----~----~------~----~------~--~---