# Accessing children of children using has\_many?

**URL:** <https://www.ruby-forum.com/t/accessing-children-of-children-using-has-many/110181>\
**Category:** Rails\
**Created:** [July 24, 2007, 12:20am UTC](https://www.ruby-forum.com/t/accessing-children-of-children-using-has-many/110181 "2007-07-24T00:20:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Astorian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Astorian](https://www.ruby-forum.com/u/Astorian)\
**Post date:** [July 24, 2007, 12:20am UTC](https://www.ruby-forum.com/t/accessing-children-of-children-using-has-many/110181/1 "2007-07-24T00:20:42Z")

</div>

Hello. Thanks in advance for your help. What is the slickest way of  
accessing children of children.

My understanding is that you can use “Parent.children” to get the  
children of an object, but Parent.children.children wont work (while  
Child.children will, but doesn’t help)

## Right now I’m using something like this

class Challenge \< ActiveRecord::Base

## has\_many :challengers, :dependent =\> :destroy has\_many :posts, :finder\_sql =\> ‘SELECT p.\* FROM posts p LEFT JOIN challengers chr ON p.challenger\_id=chr.id WHERE chr.challenge\_id = #{id}’ …

to get from Challenges, through Challengers, to Posts, so I can write  
Challenger.posts, but now I am running into trouble while trying to  
extend the association.

Any thoughts? Sorry if this is an ignorant question. I’m very new to  
this.

John

---

<div class="post-metadata">

**Author:** ![Astorian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Astorian](https://www.ruby-forum.com/u/Astorian)\
**Post date:** [July 24, 2007, 3:58am UTC](https://www.ruby-forum.com/t/accessing-children-of-children-using-has-many/110181/2 "2007-07-24T03:58:58Z")

</div>

On Jul 23, 2007, at 6:17 PM, Astorian wrote:

> Any thoughts? Sorry if this is an ignorant question. I’m very new to  
> this.
> 
> John

Here’s where the language conventions of Rails (ActiveRecord) really  
shine. Re-read your explanation and then this possible solution:

class Challenge \< ActiveRecord::Base  
has\_many :challengers, :dependent =\> :destroy  
has\_many :posts, :through =\> :challengers  
end

This assumes that:  
class Challenger \< ActiveRecord::Base  
belongs\_to :challenge  
has\_many :posts  
end

class Post \< ActiveRecord::Base  
belongs\_to :challenger  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
