I was wondering how to initialize and set a global variable in rails. For example if I was building a pizza delivery system and I want the admin to be able to "close" and "open" the place whenever he pleases.
-
have you figured out yet?William Weckl– William Weckl2015-12-01 17:35:45 +00:00Commented Dec 1, 2015 at 17:35
-
1There doesn't seem to be an explicit way to do this. Hence I believe that managing your variable through the database is the most sensible solution.Amr Abdelraouf– Amr Abdelraouf2015-12-02 18:56:30 +00:00Commented Dec 2, 2015 at 18:56
3 Answers
Setting a global variable is simple, just set it $open = false but that won't help you much in a live application because your application will probably be running across multiple processes (each with its own memory, and therefore its own global variables).
The simplest place to start is to just store this state in your database, and check it on each request that comes in where it's relevant.
Comments
$pizza_store = :open
That's all. It's global so it doesn't need to be in any sort of namespace, but I would rethink using global variables for any reason.
Start with code school or something similar to learn basic Ruby, then try a more complete tutorial (the Michael Hartl one is good) and learn 'the Rails way' - because if you want to do Rails, you have to do it their way, or you're going to quickly get frustrated.