# Has\_many :through

**URL:** <https://www.ruby-forum.com/t/has-many-through/111379>\
**Category:** Rails\
**Created:** [August 6, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/has-many-through/111379 "2007-08-06T17:17:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 6, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/has-many-through/111379/1 "2007-08-06T17:17:32Z")

</div>

Hello,  
I have 4 tables Provinces, Districts, Cities and Municipalities.  
A Province has many Districts. A district has many Cities, and a City  
has  
many Municipalities.

With this code :

class Province \< ActiveRecord::Base

# relationship

has\_many :districts  
has\_many :cities, :through =\> :districts  
end

I can do this :  
@province = Province.find(:first)  
@province.cities.first.name

I would like to go one step further and be able to do  
@province.districts.first.name

Is it possible?  
How can I do that?

Thanks for sharing your experience.  
Thomas.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 6, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/has-many-through/111379/2 "2007-08-06T18:00:43Z")

</div>

Oups!What I would like to do is :  
@province.municipalities.first.name

Thanks for pointing that out, Jon.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 6, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/has-many-through/111379/3 "2007-08-06T17:43:38Z")

</div>

Thomas B. wrote:

> has\_many :cities, :through =\> :districts  
> How can I do that?
> 
> Thanks for sharing your experience.  
> Thomas.

Unless there’s a typo in your code somewhere, I don’t think there’s any  
reason that you shouldn’t be able to do that now, with what you’ve  
already got.

–  
[http://www.5valleys.com/](http://www.5valleys.com/)

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 6, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/has-many-through/111379/4 "2007-08-06T18:39:49Z")

</div>

AR doesn’t support nested has\_many :through. You can try following :

class Province \< ActiveRecord::Base

# relationship

has\_many :districts  
has\_many :cities, :through =\> :districts, :include =\>  
:municipalities

def municipalities  
self.cities.map(&:municipalities)  
end  
end

Please note that this could nuke your memory if you’re dealing with  
large datasets.

On 8/6/07, Thomas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Hello,  
> > > has\_many :cities, :through =\> :districts  
> > > How can I do that?  
> > > [http://www.5valleys.com/](http://www.5valleys.com/)
> > 
> > > >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 7, 2007, 1:12am UTC](https://www.ruby-forum.com/t/has-many-through/111379/5 "2007-08-07T01:12:35Z")

</div>

Although it does with the has\_many\_through plugin [http://  
[code.torchbox.com/svn/rails/plugins/nested\_has\_many\_through/](http://code.torchbox.com/svn/rails/plugins/nested_has_many_through/)] 🙂

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 6, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/has-many-through/111379/6 "2007-08-06T19:49:59Z")

</div>

Thanks for your answer!Thomas.

---

<div class="post-metadata">

**Author:** ![tbaustert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@tbaustert](https://www.ruby-forum.com/u/tbaustert)\
**Post date:** [August 7, 2007, 1:22am UTC](https://www.ruby-forum.com/t/has-many-through/111379/7 "2007-08-07T01:22:35Z")

</div>

I wouldn’t really suggest using that plugin if you’re not on stable  
rails release.

On 8/7/07, Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > has\_many :cities, :through =\> :districts, :include =\> :municipalities
> > 
> > > > Thomas B. wrote:
> > > > 
> > > > > has\_many :districts  
> > > > > Is it possible?  
> > > > > [http://www.5valleys.com/](http://www.5valleys.com/)
> > 
> > –  
> > Cheers!
> > 
> > - Pratikhttp://m.onkey.org
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)
