# Rails 2 caching?

**URL:** <https://www.ruby-forum.com/t/rails-2-caching/154991>\
**Category:** Rails\
**Created:** [December 5, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/rails-2-caching/154991 "2008-12-05T22:37:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [December 5, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/rails-2-caching/154991/1 "2008-12-05T22:37:06Z")

</div>

There’s a part of my code that really needs to get the latest value from  
the database.

But AR in Rails 2 seems to have some kind of caching, so if I execute a  
Model.find statement with the exact same arguments, it won’t go to the  
database, it’ll just return the same value it got last time?

At least that’s the only way I can explain the weird behavior I am  
seeing. This seems like rather an evil thing to turn on by default, but  
at any rate, if I’m right about what’s going on… how do I disable this  
for an individual Model.find command, where I really need to get fresh  
data?

Jonathan

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [December 5, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/rails-2-caching/154991/2 "2008-12-05T23:19:48Z")

</div>

On Dec 5, 3:37 pm, Jonathan R. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> There’s a part of my code that really needs to get the latest value from  
> the database.
> 
> But AR in Rails 2 seems to have some kind of caching, so if I execute a  
> Model.find statement with the exact same arguments, it won’t go to the  
> database, it’ll just return the same value it got last time?

Actually, that’s only true during a single action (request-response  
cycle). Once your action is over, the cache is cleared.

> At least that’s the only way I can explain the weird behavior I am  
> seeing. This seems like rather an evil thing to turn on by default, but  
> at any rate, if I’m right about what’s going on… how do I disable this  
> for an individual Model.find command, where I really need to get fresh  
> data?

You should always be getting new data. If you need to get fresh data  
within an action,  
you can reload the model.

@order.reload

or

@order.products(true) # reloads associated products collection

> ## Jonathan
> 
> Posted viahttp://www.ruby-forum.com/.

Hope that helps?

Jeff

[www.purpleworkshops.com](http://www.purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [December 8, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/rails-2-caching/154991/3 "2008-12-08T16:27:15Z")

</div>

That does help, thanks. Sometimes I need fresh data within an action  
even. Or for my tests–should the sql querries be cached during tests?  
Is the semantics for what is cached when documented anywhere?

I also discovered “ModelName.uncached do”, which is helpful. But  
@individual\_model.reload is good too, along with  
@individual\_model.reset–although I’m not sure #reset will actually  
prevent re-use of the sql cache upon reload.

Some documentation of all this stuff would be really helpful, if anyone  
who understands it for sure wants to write some.

Jeff C. wrote:

> Actually, that’s only true during a single action (request-response  
> cycle). Once your action is over, the cache is cleared.
> 
> You should always be getting new data. If you need to get fresh data  
> within an action,  
> you can reload the model.
> 
> @order.reload
> 
> or
> 
> @order.products(true) # reloads associated products collection
