# Has\_many :through another has\_many :through

**URL:** <https://www.ruby-forum.com/t/has-many-through-another-has-many-through/178251>\
**Category:** Rails\
**Created:** [October 31, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/has-many-through-another-has-many-through/178251 "2009-10-31T23:45:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [October 31, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/has-many-through-another-has-many-through/178251/1 "2009-10-31T23:45:34Z")

</div>

I need to access all instances of a particular object that is a has\_many  
through two other classes. It’s easier to show what I mean with some  
example classes…

Here’s what I have:

class ClassOne  
has\_many :class\_twos  
has\_many :class\_threes, :through =\> :class\_twos  
end

class ClassTwo  
belongs\_to :class\_one  
has\_many :class\_threes  
end

class ClassThree  
belongs\_to :class\_two  
has\_many :class\_fours  
end

class ClassFour  
belongs\_to :class\_three  
end

This lets me use @class\_one.class\_twos and @class\_one.class\_threes, but  
I can’t figure out a way to access all the class\_fours that are  
associated with @class\_one THROUGH the two other classes. I thought that  
this would work:

class ClassOne  
has\_many :class\_twos  
has\_many :class\_threes, :through =\> :class\_twos  
has\_many :class\_fours, :through =\> :class\_threes  
end

But it doesn’t…any idea how I can do this, without writing a method  
that grabs all the class\_threes associated with class\_one and then makes  
an array of all class\_fours associated with it? I know I’m probably  
explaining this weird…

Anyway, any help is appreciated.

---

<div class="post-metadata">

**Author:** ![pond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7ab992/32.png) [@pond](https://www.ruby-forum.com/u/pond)\
**Post date:** [November 1, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/has-many-through-another-has-many-through/178251/2 "2009-11-01T14:29:15Z")

</div>

The most excellent nested has\_many though lets you do what you want.

> **[GitHub - ianwhite/nested\_has\_many\_through: rails plugin that allows has\_many...](https://github.com/ianwhite/nested_has_many_through)**
>
> rails plugin that allows has\_many :through to go through other has\_many :throughs - GitHub - ianwhite/nested\_has\_many\_through: rails plugin that allows has\_many :through to go through other has\_man...

best regards,  
Tor Erik L.  
[http://tel.jklm.no/](http://tel.jklm.no/)
