# When to set a class attribute variable during boot

**URL:** <https://www.ruby-forum.com/t/when-to-set-a-class-attribute-variable-during-boot/156619>\
**Category:** Rails\
**Created:** [December 31, 2008, 1:31am UTC](https://www.ruby-forum.com/t/when-to-set-a-class-attribute-variable-during-boot/156619 "2008-12-31T01:31:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcarneiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@dcarneiro](https://www.ruby-forum.com/u/dcarneiro)\
**Post date:** [December 31, 2008, 1:31am UTC](https://www.ruby-forum.com/t/when-to-set-a-class-attribute-variable-during-boot/156619/1 "2008-12-31T01:31:30Z")

</div>

I have an ActiveRecord model with a cattr\_accessor. The class  
attribute is set up like this:

class MyModel \< ActiveRecord::Base  
cattr\_accessor :my\_attribute  
end

Because I need to give #my\_attribute environment-specific values, I  
try to set this attribute in environments/development.rb like so:

MyModel.my\_attribute = 3

But this leads to odd and erratic behavior when #my\_attribute is  
called from a controller. Sometimes MyModel.my\_attribute returns 3,  
but sometimes it returns nil.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![dcarneiro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e36b37/32.png) [@dcarneiro](https://www.ruby-forum.com/u/dcarneiro)\
**Post date:** [December 31, 2008, 2:02am UTC](https://www.ruby-forum.com/t/when-to-set-a-class-attribute-variable-during-boot/156619/2 "2008-12-31T02:02:33Z")

</div>

Daniel C. wrote:

> MyModel.my\_attribute = 3
> 
> But this leads to odd and erratic behavior when #my\_attribute is  
> called from a controller. Sometimes MyModel.my\_attribute returns 3,  
> but sometimes it returns nil.

In development mode, models get reloaded on every request,  
so your variable won’t persist. You’ll have to set it in  
in a persistent class or module, in the global context,  
or in the database or session.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
