# Asp's application object rails equivalent?

**URL:** <https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837>\
**Category:** Rails\
**Created:** [April 11, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837 "2006-04-11T19:38:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 11, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/1 "2006-04-11T19:38:03Z")

</div>

In asp there’s a built’in object, Application, which behaves like the  
session, but it allows you to share information among all users of the  
application.

([http://www.microsoft.com/windows2000/en/server/IIs/default.asp?url=/windows2000/en/server/IIs/htm/asp/vbob6zkv.htm](http://www.microsoft.com/windows2000/en/server/IIs/default.asp?url=/windows2000/en/server/IIs/htm/asp/vbob6zkv.htm))

It’s just like if every user would be accessing the same session.

what is the rails equivalent?

And if there is no such thing, how would you implement such a thing?  
(taking ito account that it would arouse some concurrency issues, in  
fact asp’s Application object has a lock and unlock method for that  
purpose

Saludos

Sas

---

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 11, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/2 "2006-04-11T20:24:21Z")

</div>

There are a number of load-once files. A logical place is  
environment.rb.  
Consider placing something like this at the end of environment.rb:

# Include your application configuration below

include ‘thread’  
mutex = Mutex.new

module Settings  
SETTINGS = {}  
SETTINGS[:additional\_snippets] = 5

def config(config\_var)  
mutex.synchronize do  
SETTINGS[config\_var]  
end  
end  
end

Then in other code, include Settings and:

snippets = Settings::config(:additional\_snippets)

## I tested all except the mutex part.

View this message in context:  
[http://www.nabble.com/asp's-application-object-rails-equivalent--t1433258.html#a3867558](http://www.nabble.com/asp%27s-application-object-rails-equivalent--t1433258.html#a3867558)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 11, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/3 "2006-04-11T20:37:16Z")

</div>

Sorry. The concurrency code I originally posted won’t work. This appears  
better.

# Include your application configuration below

require ‘thread’

module Settings  
MUTEX = Mutex.new  
SETTINGS = {}  
SETTINGS[:additional\_snippets] = 5

def config(config\_var)  
MUTEX.synchronize do  
SETTINGS[config\_var]  
end  
end

## def config=(var) MUTEX.synchronize do SETTINGS[config\_var] = var end end end

View this message in context:  
[http://www.nabble.com/asp's-application-object-rails-equivalent--t1433258.html#a3867871](http://www.nabble.com/asp%27s-application-object-rails-equivalent--t1433258.html#a3867871)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 12, 2006, 1:33am UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/4 "2006-04-12T01:33:15Z")

</div>

This was one of the original suggestions and is, in fact, how I am doing  
it.  
I have a settings table with two fields:

setting\_name, :string  
setting\_value, :string

Here is the model:

class Setting \< ActiveRecord::Base  
def self.  
return self.find\_by\_setting\_name(index.to\_s).setting\_value  
end

def self.[]=(index, value)  
setting = self.find\_by\_setting\_name(index.to\_s)  
setting.update\_attribute(‘setting\_value’, value)  
end  
end

With this I can do:

number\_of\_snippets = Setting[:article\_paragraphs].to\_i

## A possible enhancement is to keep some type information so the explicit conversion is not required.

View this message in context:  
[http://www.nabble.com/asp's-application-object-rails-equivalent--t1433258.html#a3873013](http://www.nabble.com/asp%27s-application-object-rails-equivalent--t1433258.html#a3873013)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 12, 2006, 12:26am UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/5 "2006-04-12T00:26:11Z")

</div>

On 12/04/2006, at 6:21 AM, s.ross wrote:

> There are a number of load-once files. A logical place is  
> environment.rb.  
> Consider placing something like this at the end of environment.rb:

Of course, that won’t work with fastcgi, which uses a multi process  
model, or an application server cluster. Either use the database or  
Drb. I recommend the database, it’s easier, just create a settings  
table and use a before\_filter to get the records. Sure it’s an extra  
query, but it shouldn’t matter for a decent DBMS.

–  
Phillip H.  
removed\_email\_address@domain.invalid  
[http://www.sitharus.com/](http://www.sitharus.com/)

---

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 12, 2006, 3:18am UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/6 "2006-04-12T03:18:08Z")

</div>

Thanks for the prompt answers

In fact, I was thinking about some way to implement a quick and dirty  
cache system, specially for those little combo boxes that takes so many  
trips to the DB to fill.

in this thread I explain what I had n mind (when I coulnd’t find the  
original thread I thought I haven’t posted it at all… sorry for the  
duplication)

[http://www.ruby-forum.com/topic/61795#new](http://www.ruby-forum.com/topic/61795#new)

So, what would be the best way to implement such a thing???

I guess that if I keep the cache relatively small -invalidating it on a  
time-elapsed or in a fixed lenght basis-, I can load it all on the first  
access to the cache, and then in one trip to the db I would get several  
items. Even thought I would be going to the DB, it would be an  
improvement (well, that depends on wheter retrieving a hundred items in  
one trip is faster thatn retrieving ten in ten trips)

Saludos

Sas

PS: Before you tell me, I promess I’ll study rails cache system 😉

---

<div class="post-metadata">

**Author:** ![opensas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e99b99/32.png) [@opensas](https://www.ruby-forum.com/u/opensas)\
**Post date:** [April 12, 2006, 5:30am UTC](https://www.ruby-forum.com/t/asps-application-object-rails-equivalent/93837/7 "2006-04-12T05:30:56Z")

</div>

## I’m in agreement with the poster who mentioned that optimizing should wait until you know you have a performance bottleneck. If you are really set on a cache, look into memcached ([Peak Obsession](http://weblog.rubyonrails.com/articles/2006/01/20/caching-models-with-memcached)). It might fit the bill.

View this message in context:  
[http://www.nabble.com/asp's-application-object-rails-equivalent--t1433258.html#a3875108](http://www.nabble.com/asp%27s-application-object-rails-equivalent--t1433258.html#a3875108)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).
