Forum: Ruby on Rails Managing "application scope" data - best practices

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-12 01:10

I have some data that I need to be able to see across the application.
For J2EE people out there, I need something that I want to have
available in the "application scope" of my app.

What is the best way to handle this?

I'm trying to access a class variable that I created in my controller
from my view (to no avail).

Create a class variable in my helper class and use that?

How do you reference a class variable from an object of another class?

54532f023496410e0d7b1add5561ba45?d=identicon&s=25 Manuel Holtgrewe (Guest)
on 2006-04-12 01:21
(Received via mailing list)
Hi, Wes.

What exactly are you trying to do? Basically speaking, Helpers are
the thing you want to use if you want to share functionality across

If you want to share a class variable (e.g. a "public static foo" in
Java) across the application, then you can simply declare it the
following way (includes a class method that returns an arbitrary value):

class MyFoo
   FOO_BAR = 'BAZ!'

     return "BAZ AGAIN!"

Place this in a file called "my_foo.rb" and you should be able to
access it by calling "" or "MyFoo::FOO_BAR" anywhere in your
application (Rails will load the MyFoo class automatically).

25bbc96d9c53647354cb724e744b2222?d=identicon&s=25 Greg Freemyer (Guest)
on 2006-04-12 01:51
(Received via mailing list)
On 4/11/06, Manuel Holtgrewe <> wrote:
> class MyFoo
> *m

Does that really work?

I thought each instance of ruby had its own collection of variables,
so the above would fail if you ever modify the variable.

I think you need to get the variable into a shared memory/db/memcache
etc. for it to work the way you would expect.

Personally I use session variables, but not any application wide vars.

Greg Freemyer
The Norcross Group
Forensics for the 21st Century
B072fc2897196b3bafcd337bc9c91e2d?d=identicon&s=25 Sas Sas (opensas)
on 2006-04-12 03:26
I had a similar question about some kind of Application object, like in

So maybe this thread would help you


6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-12 18:16
(Received via mailing list)
On Apr 11, 2006, at 4:10 PM, Wes Gamble wrote:

> Create a class variable in my helper class and use that?
> How do you reference a class variable from an object of another class?
> Thanks,
> Wes


	I posted this as a tip a few days ago but no-one seemed to notice
it. The follwing is a nice way to get application wide context hashes
or even custom classes. Hope it helps you.


-----Easy AppServer----------------

	An easy way to get yourself an application wide context is to use
drb. Ara Howard has written an awesome library called slave that
takes care of forking a child process to run your drb server in and
then doing a heartbeat back and forth from your rails app to the
application wide classes(AppServer from here on out.). This heartbeat
is needed so that when rails starts, the AppServer starts and when
rails stops the AppServer stops because the heartbeat doesn't respond

	This slave library allows you to publish whatever class you want as
an application wide class. This means that all your requests can get
the same application wide vars or objects no matter how many fcgi's
or mongrel's you have serving up requests. You could also just as
easily serve up an app wide array or hash. Lets look at a small
example of this.

	Get the slave library here:

	Now lets publish a simple hash that we can use application wide. Put
the following in your environment.rb file so it starts up when rails
starts up.

$application_wide_hash =

	Thats all. Now you can use $application_wide_hash anywhere in your
rails apps and it will behave just like a hash. So you are still
responsible for making suitable hash keys that you can use between
requests and still distinguish from one another. But since you can
store any object in a ruby hash you can store anything you want in
this $application_wide_hash. Just make a unique key and store it in
the users session. Maybe something like this:

    key = "#{}-#{}-#{rand}
    session[:app_server_key] = key

    $application_wide_hash[key] =

	Then on the next request you can get that object back with this:


	This approach allows for a very flexible application wide context.
It's uses are only limited to your creativity ;) Of course you might
want to publish a different class then a hash in this AppServer. If
you want to do that then just replace this:

$application_wide_hash =

with this:

$application_wide_hash =

	The slave library will take care of forking a background process and
starting a drb server inside said process. Then the heartbeat takes
care of shutting down the AppServer when you shut down rails so you
don't get orphaned AppServers. Of course all the context in your
$application_wide_hash will be lost when you restart or shutdown
rails and you are responsible for deleting things out of the
$application_wide_hash when you are done with them.

	If you really want the $application_wide_hash to be persistent
between restarts of rails then you will need to do a little custom
coding yourself. With a bit of hacking you can hook into the
heartbeat code and marshall the $application_wide_hash to disk as
Yaml or binary and then reload that when rails starts back up.

	The objects or classes that you put in the AppServer cannot contain
procs, lambdas or anything else that cannot be marshalled by ruby and
drb. The way around this is to use DrbUndumped. I won't go into
detail about that here but you can look up resources on how to do
this with google ;)

	I have a full chapter on this technique in my forthcoming book that
goes into detail of what this process can be used for and what is
shouldn't be used for. As well as concrete examples and code that
folks might find useful if they need to do this kind of thing.

This topic is locked and can not be replied to.