Simone Di Cola
Simone Di Cola

Reputation: 351

Setting a variable based on Rails environment

I'm using rails 3 and I would like to set the storage variable based on the current environment, like below:

 #if enviroment = development or test 
    :storage => :filesystem,
    #else
    :storage => :s3,
    #end

What would your approach be? I've thought about setting an environment variable, but I think that there must be a better way.

Thanks for your time Simone

Upvotes: 1

Views: 1579

Answers (2)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

One way to do this is use the built-in environment setup in rails. The variables set in the environments directory will override the config/environment

config/environment.rb

ATTACHMENT_OPTIONS = {:storage => :filesystem}

config/environments/production.rb

ATTACHMENT_OPTIONS = {:storage => :s3}

Then later in your app, just ask ATTACHMENT_OPTIONS[:storage] -- no if/else throughout your app.

Upvotes: 9

Charles Ma
Charles Ma

Reputation: 49141

Rails.env will give you what you're looking for.

You can also use

Rails.env.development?
Rails.env.test?
Rails.env.production?
Rails.env.your_custom_environment?

To test for those specific environments.

Upvotes: 8

Related Questions