# Caching of classes non working or Howto?

**URL:** <https://www.ruby-forum.com/t/caching-of-classes-non-working-or-howto/70953>\
**Category:** Rails\
**Created:** [February 20, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/caching-of-classes-non-working-or-howto/70953 "2006-02-20T20:27:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 20, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/caching-of-classes-non-working-or-howto/70953/1 "2006-02-20T20:27:04Z")

</div>

Hello!

I m new to rails. I m playing around with it on a SuSE 10.0 box with  
ruby 1.8.2 and gems and rails and postgres. rails xyz creates a nice  
rails application, with its directory and ruby script/server runs  
webrick nicely and lets me see the page on localhost:3000.

I went with some tutorial to create a Hello page showing the time and a  
Bye-Link to link to an Bye page.

cat app/controller/say\_controller.rb  
class SayController \< ApplicationController  
def hello  
@time = Time.now  
end  
def goodbye  
end  
end

cat app/views/say/hello.rhtml

 Hello Philipp Hello from Rails 

It is \<%= @time %\>

Cu \<%= link\_to "Bye!", :action =\> "goodbye" %\>

cat app/views/say/goodbye.rhtml

 Bye Philipp Going home 

Time was \<%= @time %\>

It works without error. But in goodbye @time is always nil, empty, and  
it doesnt matter if I play around with  
config/environments/development.rb and change config.cache\_classes from  
false to true and back, nor if I run ruby script/server -c which should  
cache the classes. In my understanding so far, @time in the goodbye  
method should be the living alive instance variable of my living view  
controller, right? It is living beyond the lifetime of the single  
request?! So it should have the value from the hello method from the  
invocation (and creation of the controller) before. But like I said,  
whatever I m doing it is always nil.

What am I missing?

Regards,  
Philipp

---

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 20, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/caching-of-classes-non-working-or-howto/70953/2 "2006-02-20T20:35:45Z")

</div>

Philipp Ott wrote:

> It works without error. But in goodbye @time is always nil, empty, and  
> it doesnt matter if I play around with  
> config/environments/development.rb and change config.cache\_classes from  
> false to true and back, nor if I run ruby script/server -c which should  
> cache the classes. In my understanding so far, @time in the goodbye  
> method should be the living alive instance variable of my living view  
> controller, right? It is living beyond the lifetime of the single  
> request?! So it should have the value from the hello method from the  
> invocation (and creation of the controller) before. But like I said,  
> whatever I m doing it is always nil.
> 
> What am I missing?
> 
> Regards,  
> Philipp

Instance vars do not live across invocations - a new instance is created  
with each request (this screwed me up for a while too). Caching helps  
when you are accessing the same page, not the same controller. To save  
it, you can put it in the session, session[:time] = Time.now, and then  
retrieve it later. There are other ways as well.

Keith

---

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 20, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/caching-of-classes-non-working-or-howto/70953/3 "2006-02-20T20:45:37Z")

</div>

Hi!

Keith L. wrote:

> Philipp Ott wrote:
> 
> > controller, right? It is living beyond the lifetime of the single  
> > request?! So it should have the value from the hello method from the
> 
> Instance vars do not live across invocations - a new instance is created  
> with each request (this screwed me up for a while too).

Thank you for the revelation 🙂 Is any part of a rubyrails application  
surviving the request-loop? Or none, like in traditional cgi-programming  
and session-state managment handles such tasks. Could I write some  
ruby-threaded low frequency code withhin the application that checks  
something and changes the application behaviour?

Regards,  
Philipp

---

<div class="post-metadata">

**Author:** ![Philipp\_Ott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e8cbc/32.png) [@Philipp\_Ott](https://www.ruby-forum.com/u/Philipp_Ott)\
**Post date:** [February 20, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/caching-of-classes-non-working-or-howto/70953/4 "2006-02-20T22:17:17Z")

</div>

Hi Philipp,

your _classes_ are cached but instances are not.

Seeing as you’re new to rails I’ll give you a simple ‘rule’ (which  
isn’t the _only_ rule, just the simplest one until you understand how  
it all works well enough to break the rule):

Any non-model information you want to persist across requests should  
be stored in the session.

HTH,  
Trevor
