Active Record and object identity

It appears from a simple test that if I perform two queries (even the
same query twice in a row) that return the same row from the database, I
get two distinct copies of the same object.

Is there a way to turn on some sort of identity-based caching with AR?
Surely I’m missing something simple, but I can’t seem to find any
documentation on this.

Thanks!
-d

Sorry to re-post…but does anyone have information on this?

Dave W. wrote:

It appears from a simple test that if I perform two queries (even the
same query twice in a row) that return the same row from the database, I
get two distinct copies of the same object.

Is there a way to turn on some sort of identity-based caching with AR?
Surely I’m missing something simple, but I can’t seem to find any
documentation on this.

Thanks!
-d

On 14/12/2005, at 4:09 AM, Dave W. wrote:

It appears from a simple test that if I perform two queries (even the
same query twice in a row) that return the same row from the
database, I
get two distinct copies of the same object.

Is there a way to turn on some sort of identity-based caching with AR?
Surely I’m missing something simple, but I can’t seem to find any
documentation on this.

You are loading twice, so you get two objects. Rails doesn’t cache at
all in development mode. I’m not sure about production mode, but this
is expected behaviour in development mode.


Phillip H.
removed_email_address@domain.invalid

On 11/28/05, Dave W. removed_email_address@domain.invalid wrote:

It appears from a simple test that if I perform two queries (even the
same query twice in a row) that return the same row from the database, I
get two distinct copies of the same object.

Yes, you’ll have two instances of the same object, but their ==
methods will return what you’d expect. Ensuring one instance is a lot
harder than it seems at first glance, without introducing some kind of
PersistenceSession ala hibernate’s Session.


Cheers

Koz

Thanks for the responses…mostly just making sure I wasn’t missing
something obvious. This helps!

On 13-dec-2005, at 21:33, Michael K. wrote:

On 11/28/05, Dave W. removed_email_address@domain.invalid wrote:

It appears from a simple test that if I perform two queries (even the
same query twice in a row) that return the same row from the
database, I
get two distinct copies of the same object.

Yes, you’ll have two instances of the same object, but their ==
methods will return what you’d expect. Ensuring one instance is a lot
harder than it seems at first glance, without introducing some kind of
PersistenceSession ala hibernate’s Session.
I’d also say it’s difficult with the way AR works right now
(especially - the handling of joined attributes). Although I think
it’s sort of implied that you might be getting it for free if you
already have ORM. But then again it would become a mapper.

If you want identity caching take a look a cacheAR.
http://rubyforge.org/projects/muravey-tools/

Julian ‘Julik’ Tarkhanov
me at julik.nl