I'm in the process of upgrading a Rails app from major version 3 to 4. In the previous version we could tap into the boot process by passing a block to config.before_initialize
in our Application
class. From here I could specify options like rails c -some_cool_switch
and have access to it from ARGV
.
In Rails 4 I cannot even get to the pry line in this block as I'm getting the following error invalid option: --foobar (OptionParser::InvalidOption)
.
I'm about to dive into the source code to figure out what I can do short of monkey patching and will report back. I was hoping that someone else has a better solution or at least an idea that I'm going up the wrong tree.
I can always fallback to so something like SOME_ENV_VAR=some_cool_switch rails c
and just use another ENV var but I'd like to know my options and if this is the better approach or not.
The railties
gem has changed. Previously on Rails 3 the command line arguments were not parsed until start
was called on the Console
. Now the command line is parsed before the app initializes and the options are strict so an error is raised.
In Rails 4, is it possible to pass arguments to the console like rails c --some_arg
, and if so, how?
You should specify the arguments like below,
➜ test_app git:(master) ✗ RAILS_E=rails_test rails c -- --rails_env_test test
Loading development environment (Rails 4.2.1)
[1] pry(main)> ARGV
=> ["--rails_env_test", "test"]
[2] pry(main)>
or
➜ test_app git:(master) ✗ RAILS_E=rails_test rails c -- --rails_env_test=test
Loading development environment (Rails 4.2.1)
[1] pry(main)> ARGV
=> ["--rails_env_test=test"]
[2] pry(main)>
Then you can process the ARGV to get the passed values. Please let me know if you need more on this.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments