4

I want to instantiate an array that is accesible trough all the application, the array may change while the application is running but it will be re-generated when the application starts again.

I have though about putting that array in the ApplicationController but it will die once the request is over or not ?; and I just need it to populate it once every time the app starts running not every time a controller action is called.

The array is populated from the database and that has to be already loaded.

Thanks in advance for any guidance.

6
  • If you do use a global (remember it is just PER-PROCESS), at least give it a separate name-space, e.g. it might be called MyGlobals::stats or whatnot. But best to likely wrap it within a singleton object (no, not that type of singleton...) then: MyStats.add_stat(etc) Commented Mar 17, 2011 at 16:41
  • 1
    If you have multiple applications running should this same array be accessible to all of them? For example, on a server you might have 5 rails apps running at the same time, and none of them share memory. Commented Mar 17, 2011 at 16:41
  • I forgot to mention the array is populated from the database so that has to be intialized already. Commented Mar 17, 2011 at 16:43
  • You mention that the array can change while the application is running. Is this something you will do manually with an assignment or do you expect it to change in some other way? Commented Mar 17, 2011 at 16:50
  • to Pan : The array does not has to be accesible to all threads, because is a very low concurrence application. Commented Mar 17, 2011 at 16:52

3 Answers 3

5

Create a file inside you config/initializers called whatever-you-want.rb and place you code there.

THIS_IS_AN_ARRAY = [1,2,3,4]

You should then be able to access THIS_IS_AN_ARRAY all over your application.

Sign up to request clarification or add additional context in comments.

Comments

4

You can create a simple class to keep this information for you. For example, you can add the following to config/initializers/my_keep.rb:

class MyKeep
  def self.the_array
    @@the_array ||= # Execute the SQL query to populate the array here.
  end

  def self.add element
    if @@the_array
      @@the_array << element
    else
      @@the_array = [element]
    end
  end
end

In your application, the first time you call MyKeep.the_array the array will be populated from the database, so you can even do this in the same file or in an after_initialize block in your application.rb file. You'll then be able to add the the array with MyKeep.add(element) and you'll be able to get the array value with MyKeep.the_array. This class should not get re-set on every request.

2 Comments

actually it is a model it does not has to be an array, I am preloading documents from couchdb so that I can do a full text search.
That fine. You can modify the class however you want. It's not necessary that you use an array. This solution allows you to keep a variable that is initially pre-loaded from a DB and add to it throughout the application life-cycle. You should modify it to fit your use case.
1

You can use a yaml configuration file.

See this railscast

http://railscasts.com/episodes/85-yaml-configuration-file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.