# Is there an Application state in Rails like ASP

**URL:** <https://www.ruby-forum.com/t/is-there-an-application-state-in-rails-like-asp/73756>\
**Category:** Rails\
**Created:** [October 26, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/is-there-an-application-state-in-rails-like-asp/73756 "2006-10-26T21:35:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kelly\_Oppenheinmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b77776/32.png) [@Kelly\_Oppenheinmer](https://www.ruby-forum.com/u/Kelly_Oppenheinmer)\
**Post date:** [October 26, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/is-there-an-application-state-in-rails-like-asp/73756/1 "2006-10-26T21:35:04Z")

</div>

In classic ASP I can use the Application object to persist application  
wide values. Is there something similar built into Rails? I’m already  
aware of things like memcache.

---

<div class="post-metadata">

**Author:** ![Kelly\_Oppenheinmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b77776/32.png) [@Kelly\_Oppenheinmer](https://www.ruby-forum.com/u/Kelly_Oppenheinmer)\
**Post date:** [October 27, 2006, 1:45am UTC](https://www.ruby-forum.com/t/is-there-an-application-state-in-rails-like-asp/73756/2 "2006-10-27T01:45:14Z")

</div>

Unless I’m mistaken, anything you create in environment.rb persists  
until server restart because environment.rb is a load-once file.  
There are several plugins for storing settings (n/v pairs), but if  
you want to persist page state like WinForms on [Asp.Net](http://Asp.Net), this isn’t  
the method for you.

If you’re casting about for a way to store options and don’t want to  
go the plugin route, stick this in RAILS\_ROOT/lib and they you can  
write code like:

Options[:email\_recipients] = [‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’]

# options.rb

# 

# Provide a container for global option storage

# Both getter/setter and [] access methods are

# provided.

class Options  
@@option\_hash = {}

def self.set(option\_name, value)  
option\_name = option\_name.to\_s  
@@option\_hash[option\_name] = value  
end

def self.get(option\_name)  
option\_name = option\_name.to\_s  
@@option\_hash[option\_name]  
end

def self.[]=(option\_name, value)  
option\_name = option\_name.to\_s  
self.set(option\_name, value)  
end

def self.  
option\_name = option\_name.to\_s  
self.get(option\_name)  
end

def self.has\_key?(key)  
key = key.to\_s  
@@option\_hash.has\_key?(key)  
end

def self.key?(key)  
self.has\_key?(key)  
end  
end

---

<div class="post-metadata">

**Author:** ![Kelly\_Oppenheinmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b77776/32.png) [@Kelly\_Oppenheinmer](https://www.ruby-forum.com/u/Kelly_Oppenheinmer)\
**Post date:** [October 27, 2006, 2:33am UTC](https://www.ruby-forum.com/t/is-there-an-application-state-in-rails-like-asp/73756/3 "2006-10-27T02:33:18Z")

</div>

Hi~

On Oct 26, 2006, at 4:42 PM, s.ross wrote:

> def self.[]=(option\_name, value)  
> key = key.to\_s  
> @@option\_hash.has\_key?(key)  
> end
> 
> def self.key?(key)  
> self.has\_key?(key)  
> end  
> end

```
Keep in mind this will not work when you run more then one rails

```

backend. As each mongrel or fcgi will have a separate copy of this  
class and you are not always garaunteed that your users will always  
hit the same backend. So you will have to use the session, memcached  
or a DRb server for global state because it needs to be an external  
process that all rails backends can talk to to keep the data  
consistent. Or you also use the database to store state too.

Cheers-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![Kelly\_Oppenheinmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b77776/32.png) [@Kelly\_Oppenheinmer](https://www.ruby-forum.com/u/Kelly_Oppenheinmer)\
**Post date:** [October 27, 2006, 3:09am UTC](https://www.ruby-forum.com/t/is-there-an-application-state-in-rails-like-asp/73756/4 "2006-10-27T03:09:22Z")

</div>

Good point. This was lifted from some of my code that uses it on app  
startup to store app-wide setup info. Of course when each backend is  
fired up it is stuffed with the same information so 'sall good.  
Application state can better be preserved using memcached or dRb or  
even AR-sessions. Just don’t save too much state 🙂
