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.
63d11618e9cd62b7c5a8ac93b63111c2?d=identicon&s=25 Dave Wood (davewood)
on 2005-11-28 01: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
63d11618e9cd62b7c5a8ac93b63111c2?d=identicon&s=25 Dave Wood (davewood)
on 2005-12-13 16:09
Sorry to re-post...but does anyone have information on this?

Dave Wood 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
01d7a451018ac15518f425078ef00c40?d=identicon&s=25 Phillip Hutchings (Guest)
on 2005-12-13 21:07
(Received via mailing list)
On 14/12/2005, at 4:09 AM, Dave Wood 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 Hutchings
phillip.hutchings@sitharus.com
Efa76b164a7de4a5730e4fa397cc4425?d=identicon&s=25 Michael Koziarski (Guest)
on 2005-12-13 21:34
(Received via mailing list)
On 11/28/05, Dave Wood <dave@woodtopia.org> 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
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2005-12-14 00:45
(Received via mailing list)
On 13-dec-2005, at 21:33, Michael Koziarski wrote:

> On 11/28/05, Dave Wood <dave@woodtopia.org> 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
63d11618e9cd62b7c5a8ac93b63111c2?d=identicon&s=25 Dave Wood (davewood)
on 2005-12-14 06: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.