hassaanm
hassaanm

Reputation: 15599

Ruby on Rails - Global Variable?

I am a new Ruby on Rails user and had a question. I have an idea of what I want my Users DB to look like but was wondering whether or not I should add an additional value to it. Basically I need a variable to signal to all users that it is safe to proceed with a certain action. This variable would be persistent across all users and should be visible to all users, but I want the server to be able to change this variable as well. When programming in other languages, I would use a global variables, so I wanted to check if that is also the case here. If so, would this be the best approach for going about it: Site-Wide Global Variables in Ruby on Rails. Also, how would I update the global variables. Thanks for any help!

Upvotes: 5

Views: 3962

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176352

A global variable doesn't fit your need. It doesn't spread across all the Ruby processes. If your web server spawns 5 ruby processes to handle 5 request at the same time, the variable defined in the first process won't be visible to the others.

There are other solutions available. You can use a database and store the flag/information on the database. Otherwise, you can use a file and store the value in the file. The best solution would be an in-memory shared data source, such as memcached or Redis.

Upvotes: 7

Related Questions