# Association Caching

**URL:** <https://www.ruby-forum.com/t/association-caching/78173>\
**Category:** Rails\
**Created:** [December 3, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/association-caching/78173 "2006-12-03T15:56:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [December 3, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/association-caching/78173/1 "2006-12-03T15:56:34Z")

</div>

Hi,

I have a collection of Vehicles which belongs\_to a number of entities  
such as Manufacturer, Transmission and Fuel. When rendering individual  
Vehicles in the collection, I access vehicle.manufacturer.name and  
vehicle.transmission.name. Each time I access these properties, some SQL  
is equcuted such as:

‘SELECT \* FROM manufacturers WHERE (manufacturers.id = 1) LIMIT 1’

As this data is virtually static, is there a way to cache this data and  
still use the association vehicle.manufacturer.name?

Thanks,  
GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [December 3, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/association-caching/78173/2 "2006-12-03T23:21:40Z")

</div>

On 12/4/06, Giant C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> As this data is virtually static, is there a way to cache this data and  
> still use the association vehicle.manufacturer.name?

You can instruct AR to retrieve manufacturer and transmission data in  
the same SELECT statement as the vehicles:

Vehicle.find(:all, :include=\>[:manufacturer, :transmission])

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [December 3, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/association-caching/78173/3 "2006-12-03T23:31:56Z")

</div>

Max M. wrote:

> On 12/4/06, Giant C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > As this data is virtually static, is there a way to cache this data and  
> > still use the association vehicle.manufacturer.name?
> 
> You can instruct AR to retrieve manufacturer and transmission data in  
> the same SELECT statement as the vehicles:
> 
> Vehicle.find(:all, :include=\>[:manufacturer, :transmission])
> 
> Cheers,  
> Max

Thanks for the reply. I tried that, but it seems to be ignored when  
using the following:

find(:all,  
:readonly =\> true,  
:include =\> [:counties],  
:joins =\> “INNER JOIN tags\_vehicles a ON vehicles.id =  
a.vehicle\_id”,  
:conditions =\> “a.tag\_id IN (#{tags.map.join(', ')})”,  
:group =\> “vehicles.id HAVING COUNT(vehicles.id) =  
#{tags.size}”,  
:order =\> “vehicles.updated\_on DESC”,  
:page =\> {:size =\> page\_size, :first =\> 1, :current =\> page,  
:count =\> count})

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [December 4, 2006, 12:00am UTC](https://www.ruby-forum.com/t/association-caching/78173/4 "2006-12-04T00:00:53Z")

</div>

> Thanks for the reply. I tried that, but it seems to be ignored when  
> using the following:

Apologies, I was mistaken. It works a treat.

Thanks.
