Search code examples
ruby-on-railsrubyenvironment-variablesdevelopment-environmentproduction-environment

Best way to set environment-specific variables in rails


There are certain variables I would like to have in the development environment, and certain variables for the production environment. For example, in production mode, I might want to use a cache to speed up performance. I have couple of open-ended questions about this:

Question: What is the easiest and fastest way to set environment variables in a rails app? For example, ENV["USE_CACHE"] = true in production, and = false in development.

If you could point to a specific gem, and/or the specific files I'd need to touch, that would be most helpful. Thanks!


Solution

  • I learnt few things about ENV variables this discussion

    You can set ENV varaibles by just appending

    export USE_CACHE=true
    

    to your .bashrc file.

    ...and in your application you can use

    ENV["USE_CACHE"]
    

    , you can also check out figaro and dotenv which are application specific.

    For your purpose just use

    if Rails.env.eql?("development")
     #do stuff
    end
    

    Its upto you choose easiest and fastest way!