Forum: Ruby on Rails Active Record and object identity

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dave W. (Guest)
on 2005-11-28 02:42
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
Dave W. (Guest)
on 2005-12-13 17:09
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
Phillip H. (Guest)
on 2005-12-13 22:07
(Received via mailing list)
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
Michael K. (Guest)
on 2005-12-13 22:34
(Received via mailing list)
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
Julian 'Julik' Tarkhanov (Guest)
on 2005-12-14 01:45
(Received via mailing list)
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
Dave W. (Guest)
on 2005-12-14 07:01
Thanks for the responses...mostly just making sure I wasn't missing
something obvious.  This helps!
This topic is locked and can not be replied to.