# Self Referential question in RoR compared to Java Hibernate

**URL:** <https://www.ruby-forum.com/t/self-referential-question-in-ror-compared-to-java-hibernate/87939>\
**Category:** Rails\
**Created:** [March 7, 2007, 12:15am UTC](https://www.ruby-forum.com/t/self-referential-question-in-ror-compared-to-java-hibernate/87939 "2007-03-07T00:15:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dougd1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dougd1101](https://www.ruby-forum.com/u/dougd1101)\
**Post date:** [March 7, 2007, 12:15am UTC](https://www.ruby-forum.com/t/self-referential-question-in-ror-compared-to-java-hibernate/87939/1 "2007-03-07T00:15:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dougd1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dougd1101](https://www.ruby-forum.com/u/dougd1101)\
**Post date:** [March 7, 2007, 12:27am UTC](https://www.ruby-forum.com/t/self-referential-question-in-ror-compared-to-java-hibernate/87939/2 "2007-03-07T00:27:51Z")

</div>

Hi –

On 3/6/07, dougd1101 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In the RoR Comment model I have  
> with a default attrib as Lazy (More on Lazy to come). Then I would
> 
> 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](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](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](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![dougd1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dougd1101](https://www.ruby-forum.com/u/dougd1101)\
**Post date:** [March 7, 2007, 4:41am UTC](https://www.ruby-forum.com/t/self-referential-question-in-ror-compared-to-java-hibernate/87939/3 "2007-03-07T04:41:11Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![dougd1101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ba9def/32.png) [@dougd1101](https://www.ruby-forum.com/u/dougd1101)\
**Post date:** [March 7, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/self-referential-question-in-ror-compared-to-java-hibernate/87939/4 "2007-03-07T16:28:53Z")

</div>

Hi –

On 3/6/07, dougd1101 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](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](http://www.rubypal.com))
