# HowTo?: Eager caching of third order ActiveRecord assoc

**URL:** <https://www.ruby-forum.com/t/howto-eager-caching-of-third-order-activerecord-assoc/99612>\
**Category:** Rails\
**Created:** [July 24, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/howto-eager-caching-of-third-order-activerecord-assoc/99612 "2006-07-24T14:50:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![todorov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@todorov](https://www.ruby-forum.com/u/todorov)\
**Post date:** [July 24, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/howto-eager-caching-of-third-order-activerecord-assoc/99612/1 "2006-07-24T14:50:58Z")

</div>

Hello.

Using the “:include”, which generates a “LEFT OUTER JOIN” SQL command,  
it is possible to eagerly preload second order data objects (children)  
for a given association.

I wanted to know, if it is possible to eagerly preload and cache  
third order (or more) data objects, which are little-children or  
little-little-children. As I’ve seen. it is possible to add  
“LEFT OUTER JOIN” SQL clauses with the needed conditions,  
to fetch little-little-children data, but how to do this with  
ActiveRecord and its cache?

My models are as follow:

# Model Software

# -----------------------------

class Software \< ActiveRecord::Base  
has\_many :releases, :exclusively\_dependent =\> true  
end

# Model Release

# -----------------------------

class Release \< ActiveRecord::Base  
belongs\_to :software  
has\_many :acquisitions, :exclusively\_dependent =\> true  
end

# Model Acquisition

# -----------------------------

class Acquisition \< ActiveRecord::Base  
belongs\_to :release  
has\_and\_belongs\_to\_many :languages  
end

# Model Language

# -----------------------------

class Language \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :acquisitions  
end

Is it possible to find(), or paginate() Software-s, while  
eagerly preloading in cache their Release-s, the release  
Acquisition-s, and the supported Language-s for those releases?  
And how to do it?

Thank to all for your suggestions.

---

<div class="post-metadata">

**Author:** ![todorov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@todorov](https://www.ruby-forum.com/u/todorov)\
**Post date:** [July 24, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/howto-eager-caching-of-third-order-activerecord-assoc/99612/2 "2006-07-24T15:42:54Z")

</div>

On Monday, July 24, 2006, at 2:50 PM, Krassimir T. wrote:

> to fetch little-little-children data, but how to do this with
> 
> # Model Release
> 
> has\_and\_belongs\_to\_many :languages  
> Acquisition-s, and the supported Language-s for those releases?  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Try this…

Software.find(:all, :include=\>{:releases=\>{:aquisitions=\>:languages}})

In theory this should work, but YMMV.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![todorov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@todorov](https://www.ruby-forum.com/u/todorov)\
**Post date:** [July 24, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/howto-eager-caching-of-third-order-activerecord-assoc/99612/3 "2006-07-24T16:01:52Z")

</div>

Kevin O. wrote:

> On Monday, July 24, 2006, at 2:50 PM, Krassimir T. wrote:
> 
> > to fetch little-little-children data, but how to do this with
> > 
> > # Model Release
> > 
> > has\_and\_belongs\_to\_many :languages  
> > Acquisition-s, and the supported Language-s for those releases?  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> Try this…
> 
> Software.find(:all, :include=\>{:releases=\>{:aquisitions=\>:languages}})
> 
> In theory this should work, but YMMV.
> 
> \_Kevin  
> [www.sciwerks.com](http://www.sciwerks.com)

GREAT … IT WORKS …

Thank you very much!

---

<div class="post-metadata">

**Author:** ![todorov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@todorov](https://www.ruby-forum.com/u/todorov)\
**Post date:** [July 24, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/howto-eager-caching-of-third-order-activerecord-assoc/99612/4 "2006-07-24T16:19:59Z")

</div>

The result is OK, and retreived data is really cached.

Thank you again, Kevin!

Krassimir T. wrote:

> Kevin O. wrote:
> 
> > On Monday, July 24, 2006, at 2:50 PM, Krassimir T. wrote:
> > 
> > > to fetch little-little-children data, but how to do this with
> > > 
> > > # Model Release
> > > 
> > > has\_and\_belongs\_to\_many :languages  
> > > Acquisition-s, and the supported Language-s for those releases?  
> > > removed\_email\_address@domain.invalid  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> > 
> > Try this…
> > 
> > Software.find(:all, :include=\>{:releases=\>{:aquisitions=\>:languages}})
> > 
> > In theory this should work, but YMMV.
> > 
> > \_Kevin  
> > [www.sciwerks.com](http://www.sciwerks.com)
> 
> GREAT … IT WORKS …
> 
> Thank you very much!
