# Finding grandchildren with has\_many?

**URL:** <https://www.ruby-forum.com/t/finding-grandchildren-with-has-many/98728>\
**Category:** Rails\
**Created:** [January 30, 2006, 9:09am UTC](https://www.ruby-forum.com/t/finding-grandchildren-with-has-many/98728 "2006-01-30T09:09:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 30, 2006, 9:09am UTC](https://www.ruby-forum.com/t/finding-grandchildren-with-has-many/98728/1 "2006-01-30T09:09:37Z")

</div>

I have a table hierarchy like so:

sites-\>items-\>comments

I’d like to be able to do this in templates:

\<% for comment in @site.comments.find(:all, order=\>‘timestamp desc’) %\>

I’ve tried all the various permutations of ‘has\_many’ in site.rb I can  
think of, but can’t get it to work. I’m not even sure if ‘:include’ does  
anything - I thought it was supposed to cause a join, but the query log  
shows just ‘comments’ being queried. Anybody figured out how to do this?  
(Besides adding ‘site\_id’ to ‘comments’.)

Thanks!  
csn

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 30, 2006, 11:57am UTC](https://www.ruby-forum.com/t/finding-grandchildren-with-has-many/98728/2 "2006-01-30T11:57:31Z")

</div>

csn wrote:

> I have a table hierarchy like so:
> 
> sites-\>items-\>comments
> 
> I’d like to be able to do this in templates:
> 
> \<% for comment in @site.comments.find(:all, order=\>‘timestamp desc’) %\>

This looks like a job for :through associations, new in Rails 1.1:  
[http://rails.techno-weenie.net/tip/2005/12/23/teaching\_your\_blog\_model\_new\_tricks\_with\_has\_many\_through](http://rails.techno-weenie.net/tip/2005/12/23/teaching_your_blog_model_new_tricks_with_has_many_through)

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [January 30, 2006, 12:04pm UTC](https://www.ruby-forum.com/t/finding-grandchildren-with-has-many/98728/3 "2006-01-30T12:04:26Z")

</div>

Mark Reginald J. wrote:

> csn wrote:
> 
> > I have a table hierarchy like so:
> > 
> > sites-\>items-\>comments
> > 
> > I’d like to be able to do this in templates:
> > 
> > \<% for comment in @site.comments.find(:all, order=\>‘timestamp desc’) %\>
> 
> This looks like a job for :through associations, new in Rails 1.1:  
> [http://rails.techno-weenie.net/tip/2005/12/23/teaching\_your\_blog\_model\_new\_tricks\_with\_has\_many\_through](http://rails.techno-weenie.net/tip/2005/12/23/teaching_your_blog_model_new_tricks_with_has_many_through)
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

Nice, yep, that looks like exactly what I want to do. 🙂

csn
