Finding grandchildren with has_many?


#1

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


#2

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


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


#3

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


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

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

csn