dougd1101
2007-Mar-06 23:15 UTC
Self Referential question in RoR compared to Java Hibernate
I have a MySQL table. Its light schema as follows Comments id int not null, auto-inc. unsigned comment text, not null comment_id, int, ##This is the parent_id of the comment if it exist. some other irrelevant fields In the RoR Comment model I have def Comment < ActiveRecord::Base has_and_belongs_to_many :comments end Java hibernate comparison (high level) What I have to do in hibernate is change the hbm XML class for Comments to let hibernate know that Comments could have many children with a default attrib as Lazy (More on Lazy to come). Then I would have to edit the Comment Class to include the Set (childrenSet) that I just set up in the Comments hbm XML file. Now when I get a comment, any comment I could check the set to see if I had any children. The important part here is that as long as I had my DB session open, I would have to "Request" the childrenSet by asking for the childrenSet (ie getChildrenSet). If I never asked for the childrenSet hibernate would never get nor instantiate those objects (this is the definition of "lazy" mentioned above) So what I would like to know is two things. How would I get all children for a particular comment in RoR in the view? Is there such a feature as lazy in RoR? BTW, do you think the field named comment in my comments table could get a little tricky with all the RoR rules? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David A. Black
2007-Mar-06 23:26 UTC
Re: Self Referential question in RoR compared to Java Hibernate
Hi -- On 3/6/07, dougd1101 <dougd1101-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have a MySQL table. Its light schema as follows > > Comments > id int not null, auto-inc. unsigned > comment text, not null > comment_id, int, ##This is the parent_id > of the comment if it exist. > some other irrelevant fields > > In the RoR Comment model I have > def Comment < ActiveRecord::Base > has_and_belongs_to_many :comments > end > > > > Java hibernate comparison (high level) > > What I have to do in hibernate is change the hbm XML class for > Comments to let hibernate know that Comments could have many children > with a default attrib as Lazy (More on Lazy to come). Then I would > have to edit the Comment Class to include the Set (childrenSet) that I > just set up in the Comments hbm XML file. Now when I get a comment, > any comment I could check the set to see if I had any children. The > important part here is that as long as I had my DB session open, I > would have to "Request" the childrenSet by asking for the childrenSet > (ie getChildrenSet). If I never asked for the childrenSet hibernate > would never get nor instantiate those objects (this is the definition > of "lazy" mentioned above) > > So what I would like to know is two things. > > How would I get all children for a particular comment in RoR in the > view?@comment.children # or equivalent> Is there such a feature as lazy in RoR?The collection won''t get loaded unless you ask for it -- and even then, under some circumstances it will be loaded lazily. You can also do "eager" loading; see the :include parameter for ActiveRecord::Base.find.> BTW, do you think the field named comment in my comments table could > get a little tricky with all the RoR rules?Yes :-) In fact, your modeling needs to be fixed a bit. I''d recommend something like: Table columns: id body parent_id etc. Model file: class Comment < ActiveRecord::Base has_many :children, :class_name => "Comment", :foreign_key => "parent_id" belongs_to :parent, :class_name => "Comment", "foreign_key" => "parent_id" # etc. -- other code as needed end That way you''re not being ambiguous when you say: @comment.comment, which in your model could be either the body or the parent. Also, I don''t think you need habtm, since presumably a comment only has one parent. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
dougd1101
2007-Mar-07 03:39 UTC
Re: Self Referential question in RoR compared to Java Hibernate
Hi, That was a great post! You are absolutly correct a comment will only have at most one parent. Should have thought about it a little more. However in an earlier question I posted http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a1cf4d850d90ccb1?hl=en someone suggested I call the parent_id, comment_id. Isn''t this the Ruby way? On Mar 6, 5:26 pm, "David A. Black" <dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> Hi -- > > On 3/6/07, dougd1101 <dougd1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I have a MySQL table. Its light schema as follows > > > Comments > > id int not null, auto-inc. unsigned > > comment text, not null > > comment_id, int, ##This is the parent_id > > of the comment if it exist. > > some other irrelevant fields > > > In the RoR Comment model I have > > def Comment < ActiveRecord::Base > > has_and_belongs_to_many :comments > > end > > > Java hibernate comparison (high level) > > > What I have to do in hibernate is change the hbm XML class for > > Comments to let hibernate know that Comments could have many children > > with a default attrib as Lazy (More on Lazy to come). Then I would > > have to edit the Comment Class to include the Set (childrenSet) that I > > just set up in the Comments hbm XML file. Now when I get a comment, > > any comment I could check the set to see if I had any children. The > > important part here is that as long as I had my DB session open, I > > would have to "Request" the childrenSet by asking for the childrenSet > > (ie getChildrenSet). If I never asked for the childrenSet hibernate > > would never get nor instantiate those objects (this is the definition > > of "lazy" mentioned above) > > > So what I would like to know is two things. > > > How would I get all children for a particular comment in RoR in the > > view? > > @comment.children # or equivalent > > > Is there such a feature as lazy in RoR? > > The collection won''t get loaded unless you ask for it -- and even > then, under some circumstances it will be loaded lazily. You can also > do "eager" loading; see the :include parameter for > ActiveRecord::Base.find. > > > BTW, do you think the field named comment in my comments table could > > get a little tricky with all the RoR rules? > > Yes :-) In fact, your modeling needs to be fixed a bit. I''d > recommend something like: > > Table columns: > id > body > parent_id > etc. > > Model file: > class Comment < ActiveRecord::Base > has_many :children, :class_name => "Comment", :foreign_key => "parent_id" > belongs_to :parent, :class_name => "Comment", "foreign_key" => "parent_id" > # etc. -- other code as needed > end > > That way you''re not being ambiguous when you say: @comment.comment, > which in your model could be either the body or the parent. Also, I > don''t think you need habtm, since presumably a comment only has one > parent. > > David > > -- > Q. What is THE Ruby book for Rails developers? > A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) > (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) > Q. Where can I get Ruby/Rails on-site training, consulting, coaching? > A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
David A. Black
2007-Mar-07 15:26 UTC
Re: Self Referential question in RoR compared to Java Hibernate
Hi -- On 3/6/07, dougd1101 <dougd1101-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > That was a great post!Glad to oblige :-)> You are absolutly correct a comment will only have at most one > parent. Should have thought about it a little more. > > However in an earlier question I posted > http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a1cf4d850d90ccb1?hl=en > someone suggested I call the parent_id, comment_id. Isn''t this the > Ruby way?There''s no specific Ruby way to name columns in a relational database :-) As for ActiveRecord: Luke was telling you about the default (comment_id for a Comment association), but as he said, you can use :foreign_key to override it. I think parent is a much better thing to call a comment''s, ummm, parent than comment is. So I''d do the override. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---