I’m trying to write a Rails 3 rake task to generate config/initializers/secret_token.rb. I want to pull the application’s name instead of hard-coding it in the rake task, but @app_name is not yet populated, nor is ENV['APP_NAME']. Here’s the task’s code:
desc "Regenerate the server secret"
task :generate_secret do
include ActiveSupport
File.open('config/initializers/secret_token.rb', 'w') do |f|
f.puts "#{@app_name}::Application.config.secret_token = '#{SecureRandom.hex(30)}'"
end
end
It all works except that @app_name is blank. How can I retrieve the application name here?
You need to use the environment in the rake task. Then you can use
Rails.application.class.parent_name