Skip to content

Rails.env not set correctly in initializers when using rails console #439

Open
@rafaelfranca

Description

@rafaelfranca

From @thinkerbot on August 12, 2014 18:30

I observed that Rails.env is not set correctly within initializers when using rails console -e. For example:

$ rails -v
Rails 4.1.4
$ rails new example
$ cd example
$ cat > config/initializers/example.rb <<"DOC"
File.open("example.env", "w") {|io| io.puts Rails.env }
DOC
$ printf "puts Rails.env; exit" | rails console -e test
Loading test environment (Rails 4.1.4)
Switch to inspect mode.
puts Rails.env; exit
test
$ cat example.env
development

You can see that the initializer is picking up the 'development' as a default but the console eventually sets Rails.env to test before the prompt appears. You can work around this issue by using an ENV variable instead of the option.

$ rm example.env
$ printf "puts Rails.env; exit" | RAILS_ENV=test rails console
Loading test environment (Rails 4.1.4)
Switch to inspect mode.
puts Rails.env; exit
test
$ cat example.env
test

Copied from original issue: rails/rails#16479

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions