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?

Hi --

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 :slight_smile: 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

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?

Hi --

Hi,

That was a great post!

Glad to oblige :slight_smile:

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
:slight_smile: 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