I have two simple rake tasks — one that works and one that throws the following error:
rake aborted!
undefined method `my_config_param' for #<Rails::Application::Configuration:0x007ffba6b2d320>
Here are the tasks:
task :with_env => :environment do
Rails.application.config.my_config_param #works
end
task :without_env do
Rails.application.config.my_config_param #fails
end
It’s pretty clear why :without_env fails and I can easily wrap my call to Rails.application.config in a begin/rescue statement. But I would like to know if there’s a way I can tell a priori if the environment is missing so I can work around this more elegantly. Is there such a way? I’ve been unable to find one….
A colleague of mine noted that
$rails_rake_taskreturns true if the rails env is loaded and false otherwise. This did the trick:Rails.application.config.my_config_param if $rails_rake_task