Forum: Ruby on Rails Finding grandchildren with has_many?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
csn (Guest)
on 2006-01-30 10:09
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
Mark Reginald J. (Guest)
on 2006-01-30 12:57
(Received via mailing list)
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/teac...

--
We develop, watch us RoR, in numbers too big to ignore.
csn (Guest)
on 2006-01-30 13:04
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/teac...
>
> --
> We develop, watch us RoR, in numbers too big to ignore.


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

csn
This topic is locked and can not be replied to.