Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment-specific initializers for rails?

Can you configure rails to only run an initializer under certain environments? In my case I had to hack paperclip to work with Imagemagick on my dev box, so I have monkeypatched code I want only to apply to the development environ, not the production environment. That monkeypatch is saved as a file in config\initializers.

The guides.rubyonrails.org site does not indicate that one can do this. If I can't I suppose I just won't check this patch into my repo, but that would not be ideal.

like image 245
esilver Avatar asked Aug 05 '09 23:08

esilver


People also ask

What are the environments does the Rails have by default?

When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.

What are the environments in Rails?

Rails comes packages with 3 types of environments. Each have its own server, database, and configuration. See Rails Guides: Configuration for more information on options available to you.

What are Initializers in Rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.

How do I see environment variables in Rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.


1 Answers

You could put this in an after_initialize block in config/environments/development.rb, or just surround it with if Rails.env.development? in the initializer you already have.

I think either of these would work for you.

like image 140
Luke Francl Avatar answered Sep 24 '22 00:09

Luke Francl