# Traversing nested ActiveRecord associations

**URL:** <https://www.ruby-forum.com/t/traversing-nested-activerecord-associations/226048>\
**Category:** Rails\
**Created:** [September 8, 2009, 2:23pm UTC](https://www.ruby-forum.com/t/traversing-nested-activerecord-associations/226048 "2009-09-08T14:23:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 8, 2009, 2:23pm UTC](https://www.ruby-forum.com/t/traversing-nested-activerecord-associations/226048/1 "2009-09-08T14:23:20Z")

</div>

Let’s say…

class Town \< ActiveRecord::Base  
has\_many :streets  
has\_many :houses, :through =\> :streets  
end

class Street \< ActiveRecord::Base  
has\_many :houses  
end

Then we can find individual streets and houses like this…

@first\_town = Town.find(:first)  
@first\_towns\_first\_street = @first\_town.streets.first  
@first\_streets\_first\_house = @first\_town\_first\_street.house.first

We can also find arrays of streets and houses like this…

@first\_town\_all\_streets = @first\_town.streets  
@first\_street\_all\_houses = @first\_street.houses

But what is the correct syntax for the kind of array I’m trying to  
generate here?

@first\_town\_all\_houses = @first\_town.streets.houses

I’m presuming I can create a methods that iterates through the streets  
array, generates an array of houses for each street, and adds that  
array to a larger array of my own creation. Likewise, I’m assuming I  
can assign a town\_id to each house whenever I create it, just as I  
automatically assign it a street\_id through create/build. However, I’m  
hoping there’s an existing method that’ll just magic up the results.  
Problem is, so far I haven’t found it on either of the following  
pages:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

[http://corelib.rubyonrails.org/classes/Array.html](http://corelib.rubyonrails.org/classes/Array.html)

Thanks in advance,

Steven.

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 9, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/traversing-nested-activerecord-associations/226048/2 "2009-09-09T13:27:16Z")

</div>

OK, I added a town\_id to each house and it’s working fine, but if  
there’s a method I’ve missed that handles this automagically, it’d be  
great to know.

s.

---

<div class="post-metadata">

**Author:** ![steven\_noble](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@steven\_noble](https://www.ruby-forum.com/u/steven_noble)\
**Post date:** [September 9, 2009, 1:35pm UTC](https://www.ruby-forum.com/t/traversing-nested-activerecord-associations/226048/3 "2009-09-09T13:35:28Z")

</div>

Where is the belongs\_to :town in the Street Model?

class Town \< ActiveRecord::Base  
has\_many :streets  
has\_many :houses, :through =\> :streets  
end

class Street \< ActiveRecord::Base  
belongs\_to :town  
has\_many :houses  
end

You can then do Town.first.houses just fine.
