# Changes to class caching between Rails 0.14.3 and 1.0?

**URL:** <https://www.ruby-forum.com/t/changes-to-class-caching-between-rails-0-14-3-and-1-0/54619>\
**Category:** Rails\
**Created:** [March 10, 2006, 4:44am UTC](https://www.ruby-forum.com/t/changes-to-class-caching-between-rails-0-14-3-and-1-0/54619 "2006-03-10T04:44:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rusty\_geldmacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@rusty\_geldmacher](https://www.ruby-forum.com/u/rusty_geldmacher)\
**Post date:** [March 10, 2006, 4:44am UTC](https://www.ruby-forum.com/t/changes-to-class-caching-between-rails-0-14-3-and-1-0/54619/1 "2006-03-10T04:44:43Z")

</div>

Hi all,

We’re having a rather unique problem with class caching. We are  
implementing the strategy pattern in the following manner:

1. Set an accessor for the class, let’s call it :strategy

class Widget  
cattr\_accessor :strategy  
end

1. In environment.rb, we instantiate an object that implements the  
strategy and use the accessor to set it for the class

strat = MyStrategy.new  
Widget.strategy = strat

1. In the regular flow of the application, that strategy gets accessed  
so it can affect some behavior:

…  
Widget.strategy.do(:foo)  
…

OK, now in development mode, class caching is off. In Rails 0.14.3 this  
didn’t seem to matter – the class variable stayed set. But now  
mysteriously in Rails 1.0 I will get an exception when trying to call  
.do() on nil… ie, Widget.strategy becomes nil at some point. I’m  
assuming the class gets unloaded and then reloaded without :strategy  
ever getting set again. That’s likely because the environment never got  
to set up the class variable again.

So, in 0.14.3, did the environment get evaluated again? Or did the class  
never really get unloaded?

Does anyone know a way I can avoid this problem? Is there a way to  
control class caching on a per-class basis? Looking at “module  
Dependencies” makes me believe there is not.

Thanks for any help,  
rusty

---

<div class="post-metadata">

**Author:** ![rusty\_geldmacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@rusty\_geldmacher](https://www.ruby-forum.com/u/rusty_geldmacher)\
**Post date:** [March 10, 2006, 4:59am UTC](https://www.ruby-forum.com/t/changes-to-class-caching-between-rails-0-14-3-and-1-0/54619/2 "2006-03-10T04:59:23Z")

</div>

> Does anyone know a way I can avoid this problem? Is there a way to  
> control class caching on a per-class basis? Looking at “module  
> Dependencies” makes me believe there is not.
> 
> Thanks for any help,  
> rusty

Try this on edge rails…

class Widget \< AR::Base  
def self.reloadable?() false; end  
end

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![rusty\_geldmacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@rusty\_geldmacher](https://www.ruby-forum.com/u/rusty_geldmacher)\
**Post date:** [March 10, 2006, 4:59am UTC](https://www.ruby-forum.com/t/changes-to-class-caching-between-rails-0-14-3-and-1-0/54619/3 "2006-03-10T04:59:25Z")

</div>

> Try this on edge rails…
> 
> class Widget \< AR::Base  
> def self.reloadable?() false; end  
> end

Doh, sorry, I read that as 1.1.

---

<div class="post-metadata">

**Author:** ![rusty\_geldmacher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@rusty\_geldmacher](https://www.ruby-forum.com/u/rusty_geldmacher)\
**Post date:** [March 10, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/changes-to-class-caching-between-rails-0-14-3-and-1-0/54619/4 "2006-03-10T15:58:56Z")

</div>

Rick O. wrote:

> > Try this on edge rails…
> > 
> > class Widget \< AR::Base  
> > def self.reloadable?() false; end  
> > end
> 
> Doh, sorry, I read that as 1.1.

Hi Rick,

Thanks for the reply! Are you saying that this problem can be addressed  
if I use Rails 1.1?

Thanks,  
rusty
