# Association through 2 intermediate Models

**URL:** <https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311>\
**Category:** Rails\
**Created:** [February 25, 2009, 11:11am UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311 "2009-02-25T11:11:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 25, 2009, 11:11am UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/1 "2009-02-25T11:11:31Z")

</div>

Hey,

I have an association chain like this:

Site has\_many Feed(s) has\_many FeedEntry(/ies) has\_many Articles

where the reverse is always belongs\_to:

Article belongs\_to FeedEntry belongs\_to Feed belongs\_to Site

Now what I want is to get all articles that belong to a specific Site,  
something like Site.first.articles

I can’t figure out how to do this, or if it’s possible with  
has\_many :through  
Can anyone give me a hint please?

Thank you

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 25, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/2 "2009-02-25T13:55:13Z")

</div>

you can use through na…

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 25, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/3 "2009-02-25T14:28:16Z")

</div>

sorry, what?

On Feb 25, 1:55 pm, Priya B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 25, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/4 "2009-02-25T14:34:45Z")

</div>

On Wed, Feb 25, 2009 at 5:27 AM, sol [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sorry, what?
> 
> On Feb 25, 1:55 pm, Priya B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > you can use through na…

Hi, I recommend reading AWDwR 3ed on using ‘has\_many through’ for the  
details.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 27, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/5 "2009-02-27T21:31:51Z")

</div>

perhaps the responses could be a bit more helpful here?

sol,

I have done it through 2 level associations using through

eg.

Sites

has\_many :feed\_entries, :through=\>:feeds

I have tried with 3 levels, but never got it to work, ie, how do you  
chain the throughs. I have tried to make sure that the through is in  
place at each level, but to no avail.

Perhaps someone may be able to enlighten us.

Tonypm

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 28, 2009, 3:44am UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/6 "2009-02-28T03:44:04Z")

</div>

I don’t know if you can get there just w/HM=\>T, but you can fake out the  
last link w/a custom method. This is working for me:

class Article \< ActiveRecord::Base  
belongs\_to :feed\_entry  
end

class FeedEntry \< ActiveRecord::Base  
belongs\_to :feed  
has\_many :articles  
end

class Feed \< ActiveRecord::Base  
belongs\_to :site  
has\_many :feed\_entries  
has\_many :articles, :through =\> :feed\_entries  
end

class Site \< ActiveRecord::Base  
has\_many :feeds  
def articles  
arts = []  
self.feeds.each do |f|  
arts \<\< f.articles  
end  
# Not sure if that uniq call is necessary…  
arts.flatten.uniq  
end  
end

That gives me a my\_site.articles collection which returns the union of  
all articles attached to any feed\_entry attached to any feed attached to  
the Site.

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![sol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ebca7d/32.png) [@sol](https://www.ruby-forum.com/u/sol)\
**Post date:** [February 25, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/association-through-2-intermediate-models/161311/7 "2009-02-25T15:40:13Z")

</div>

Hi,

On Feb 25, 2:34 pm, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I recommend reading AWDwR 3ed on using ‘has\_many through’ for the  
> details.

These details are the reason I’m asking 🙂

is this:  
[http://agilewebdevelopment.com/plugins/nested\_has\_many\_through](http://agilewebdevelopment.com/plugins/nested_has_many_through)

still the only way to do this? or am I just blind.
