0

I have running rails application. I cannot restart the server.

I have an environment variable that I need to change. I tried to do it through the rails console

ENV['SCORE'] = "123"

It updates variable for the current rails c session:

ENV['SCORE']
=> "123"

But has no effect on the variable that application is using.

Is there a way to permanently change environment variable at runtime?

2
  • 1
    Your rails server and your rails console are two separate processes. Objects are not shared between them and ENV is no exception. Commented Sep 8, 2014 at 11:29
  • Thanks, @Stefan! That makes a lot of sense! Commented Oct 15, 2014 at 20:03

2 Answers 2

4

If you want to share data between processes you typically use some kind of storage. In this situation, an in-memory key/value storage like Redis (http://redis.io/) seems optimal for your use case.

Install and run Redis, then put the gem in your Gemfile.

gem "redis"

In your code connect to the database

require "redis"

client = Redis.new
client.set("score", 123)

client.get("score")
# => "123"

client.incr("score")
# => "124"
Sign up to request clarification or add additional context in comments.

Comments

0

If you create an initializer with that code

ENV['SCORE'] = "123"

the value will persist.

2 Comments

I will need to restart my server then. I am wondering if there is solution for running application?
Modifying an environment variable in one server thread won't change it for others.

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.