Smarter Eager Loading?

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

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. [email protected] 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.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

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/
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs