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.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 csn (Guest)
on 2006-01-30 09: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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-30 11: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.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 csn (Guest)
on 2006-01-30 12:04
Mark Reginald James 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.