# Caching a has\_many child's parent

**URL:** <https://www.ruby-forum.com/t/caching-a-has-many-childs-parent/127600>\
**Category:** Rails\
**Created:** [January 30, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/caching-a-has-many-childs-parent/127600 "2008-01-30T18:33:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![patnakajima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@patnakajima](https://www.ruby-forum.com/u/patnakajima)\
**Post date:** [January 30, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/caching-a-has-many-childs-parent/127600/1 "2008-01-30T18:33:45Z")

</div>

Let’s say we have two models with a plain has\_many/belongs\_to  
relationship.

class Driver  
has\_one :car  
end

class Car  
belongs\_to :driver  
end

The question is this: Why isn’t the car’s parent driver cached when we  
eager load it? So the following snippet (as impractical as it is, it’s  
just an example) would just result in one hit (as opposed to  
ActiveRecord’s current behavior, which hits the db each time that  
#driver is called).

@driver = Driver.find(:first, :include =\> :car)  
@driver.car.driver.car.driver.car.driver.car

Is there a reason for the current behavior?

---

<div class="post-metadata">

**Author:** ![patnakajima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@patnakajima](https://www.ruby-forum.com/u/patnakajima)\
**Post date:** [January 30, 2008, 8:33pm UTC](https://www.ruby-forum.com/t/caching-a-has-many-childs-parent/127600/2 "2008-01-30T20:33:17Z")

</div>

On Jan 30, 2008, at 10:54 AM, Rick O. wrote:

> > class Car
> 
> @driver.car # has\_one query  
> exact same ruby object.  
> So, you can select all the drivers and all the cars for those drivers  
> map only indexes records by their primary key. There’s a good chance  
> this plugin will be finding its way into the next release of Rails.

Rick, thanks for this explanation. I’m wondering whether the OP’s  
question was more related to the use case:

- I have a Driver
- Who has a car
- And I want to know what drivers also have that car
- And I want to know all the cars all of those drivers have

Driver.find(:first).car.drivers.cars

Question to OP: Was this actually what you had in mind or did I misread?

---

<div class="post-metadata">

**Author:** ![patnakajima](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@patnakajima](https://www.ruby-forum.com/u/patnakajima)\
**Post date:** [January 30, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/caching-a-has-many-childs-parent/127600/3 "2008-01-30T19:55:30Z")

</div>

On Jan 30, 2008 9:32 AM, Pat N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> Is there a reason for the current behavior?  
> Are you using query caching? That should only be maybe 3 queries tops:

Driver.find # eager include query  
@car.driver # belongs\_to query  
@driver.car # has\_one query

After that, the queries should be repeating and you won’t hit the  
database anymore. Even then, each association call is creating a new  
ruby object, even if that query is cached.

I have a plugin that enables a simple identity map for ActiveRecord  
within the context of a controller action:  
git://activereload.net/active\_record\_context.git . Every time a  
record is fetched from the database, it is stored in a global hash by  
ID. Future requests to receive that record will instead receive the  
exact same ruby object.

Driver.find 1 # query  
Driver.find 1 # ID map  
Driver.find 1 # ID map

The cool thing is you can preload from large queries:

Driver.find :all # query  
Driver.find 1 # ID map

So, you can select all the drivers and all the cars for those drivers  
in a single query:

@drivers = Driver.find :all  
Car.find :all, :conditions =\> {:driver\_id =\> @drivers.collect(&:id)}  
@drivers.each do |driver|  
driver.car # preloaded in the Car.find query  
end

That’s all fine and dandy, but this won’t work for has\_one  
associations since the query is by driver\_id. The plugin’s identity  
map only indexes records by their primary key. There’s a good chance  
this plugin will be finding its way into the next release of Rails.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
