# Activerecord re-sets class members to null?

**URL:** <https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362>\
**Category:** Rails\
**Created:** [August 16, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362 "2010-08-16T16:48:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 16, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/1 "2010-08-16T16:48:59Z")

</div>

I have two classes with class variables. One of them derives from active  
record, the other one does not. I initialize both members to NULL, then  
lazy-instantiate them. The class which does not derive from active  
record preserves the value correctly. The class which does derive from  
active record always seems to be re-setting it to NULL.

class Class1 ## @@thelist holds the initialized value

@@thelist = nil  
attr\_accessor :name, :id

def self.retrieveall  
if @@thelist.nil?  
@@thelist = Array.new

```
  entry = Locale.new
  entry.id = "en"
  entry.name = "english"
  @@thelist << entry

  //repeat to populate
end

@@thelist

```

end  
end

class Class2 \< ActiveRecord::Base

## anytime retrieveall is called @@thelist is always NULL. Why?

@@thelist = nil  
attr\_accessor :name, :id

def self.retrieveall  
if @@thelist.nil?  
@@thelist = Class2.all  
end

```
@@thelist

```

end  
end

---

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 16, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/2 "2010-08-16T17:25:42Z")

</div>

On Aug 16, 3:48 pm, Tomasz R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have two classes with class variables. One of them derives from active  
> record, the other one does not. I initialize both members to NULL, then  
> lazy-instantiate them. The class which does not derive from active  
> record preserves the value correctly. The class which does derive from  
> active record always seems to be re-setting it to NULL.

Most classes in your app are reloaded between requests. Is this the  
problem ?

Fred

---

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 16, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/3 "2010-08-16T17:29:48Z")

</div>

On Aug 16, 4:23 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Aug 16, 3:48 pm, Tomasz R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have two classes with class variables. One of them derives from active  
> > record, the other one does not. I initialize both members to NULL, then  
> > lazy-instantiate them. The class which does not derive from active  
> > record preserves the value correctly. The class which does derive from  
> > active record always seems to be re-setting it to NULL.
> 
> Most classes in your app are reloaded between requests. Is this the  
> problem ?

To clarify, that of course only happens in development

Fred

---

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 17, 2010, 2:10am UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/4 "2010-08-17T02:10:51Z")

</div>

> > Most classes in your app are reloaded between requests. Is this the  
> > problem ?
> 
> To clarify, that of course only happens in development

Yep, that’s it, thanks. Makes me wonder how I can control which classes  
should be reloaded. I’m trying to make something off of development.rb  
and production.rb.

---

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 17, 2010, 4:50am UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/5 "2010-08-17T04:50:10Z")

</div>

On Mon, Aug 16, 2010 at 7:10 PM, Tomasz R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yep, that’s it, thanks. Makes me wonder how I can control which classes  
> should be reloaded. I’m trying to make something off of development.rb  
> and production.rb.

Rails comes with 3 development environments already ready to go, look  
database.yml. In development and testing, all classes are always  
reloaded. In production they are always cached. How is this not  
exactly what you want? 🙂

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

---

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 18, 2010, 1:57am UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/6 "2010-08-18T01:57:16Z")

</div>

On Aug 18, 12:33 am, Tomasz R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Rails comes with 3 development environments already ready to go, look  
> > database.yml. In development and testing, all classes are always  
> > reloaded.
> 
> This does not seem to be true. In my example above, if a class does not  
> derive from ActiveRecord::Base it preserves its class attributes between  
> requests, a plausible explanation being that it’s NOT in fact reloaded.

Deriving from activerecord or not should have no incidence. How the  
particular class is loaded can influence reloading, the only thing  
that you’re likely to do by accident is requiring a file explicitly

Fred

---

<div class="post-metadata">

**Author:** ![techy\_bolek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee59a6/32.png) [@techy\_bolek](https://www.ruby-forum.com/u/techy_bolek)\
**Post date:** [August 18, 2010, 1:33am UTC](https://www.ruby-forum.com/t/activerecord-re-sets-class-members-to-null/193362/7 "2010-08-18T01:33:53Z")

</div>

> Rails comes with 3 development environments already ready to go, look  
> database.yml. In development and testing, all classes are always  
> reloaded.

This does not seem to be true. In my example above, if a class does not  
derive from ActiveRecord::Base it preserves its class attributes between  
requests, a plausible explanation being that it’s NOT in fact reloaded.

> In production they are always cached. How is this not  
> exactly what you want? 🙂  
> Very simple. I only would like to be reloading the classes that I’m  
> currently messing with. Reloading all the classes is a waste of time.
