# Smarter Eager Loading?

**URL:** <https://www.ruby-forum.com/t/smarter-eager-loading/137969>\
**Category:** Rails\
**Created:** [May 19, 2008, 2:49am UTC](https://www.ruby-forum.com/t/smarter-eager-loading/137969 "2008-05-19T02:49:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![normelton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f1d935/32.png) [@normelton](https://www.ruby-forum.com/u/normelton)\
**Post date:** [May 19, 2008, 2:49am UTC](https://www.ruby-forum.com/t/smarter-eager-loading/137969/1 "2008-05-19T02:49:14Z")

</div>

Question… if i’ve got a one-to-one relationship between Assets and  
Tags (simple example), and I do:

a = Asset.find 1, :include =\> :tag

I can do “a.tag” without hitting the database again. But “a.tag.asset”  
instantiates a new Asset object, with a new database query… even  
though it’s a reference back to the original object. Now I’ve got two  
instances of the same object, which could be confusing.

Is there a way to make ActiveRecord “smart” enough to know that an  
instance of a given record is already initialized?

Thanks,

Norman

---

<div class="post-metadata">

**Author:** ![normelton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f1d935/32.png) [@normelton](https://www.ruby-forum.com/u/normelton)\
**Post date:** [May 19, 2008, 3:22am UTC](https://www.ruby-forum.com/t/smarter-eager-loading/137969/2 "2008-05-19T03:22:01Z")

</div>

Without hacking the source code I would think not.

ActiveRecord would usually do some caching in situations like this, but  
because your initial query is :include =\> “tag”, that goes out the  
window.

9, 2008 at 10:18 AM, Norman E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to make ActiveRecord “smart” enough to know that an  
> instance of a given record is already initialized?
> 
> Thanks,
> 
> Norman
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![normelton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f1d935/32.png) [@normelton](https://www.ruby-forum.com/u/normelton)\
**Post date:** [May 19, 2008, 9:54am UTC](https://www.ruby-forum.com/t/smarter-eager-loading/137969/3 "2008-05-19T09:54:45Z")

</div>

On 19 May 2008, at 02:21, Ryan B. (Radar) wrote:

> Without hacking the source code I would think not.
> 
> ActiveRecord would usually do some caching in situations like this,  
> but because your initial query is :include =\> “tag”, that goes out  
> the window.

I believe  
[http://svn.techno-weenie.net/projects/plugins/active\_record\_context/](http://svn.techno-weenie.net/projects/plugins/active_record_context/)  
solves this problem because it caches loaded records by id (rather  
than caching the results of an sql query, keyed by that query).  
Fundamentally it would be nice if rails knew which the ‘reverse’  
association was, but right now that’s not the case.

Fred
