It can be handy to run some code whenever the rails console
command is run.
You may want to have some modules required, some variables set up, or, both
fancy and practical, default to the read replica database in production.
Rails provides a hook into the console startup with the console
block in
config/application.rb
.
Here is what it looks like to puts
out the environment:
class Application < Rails::Application
# everything else ...
console do
puts '############################################'
puts 'Connected to the #{Rails.env.upcase} console'
puts '############################################'
end
end
To avoid cluttering config/application.rb
with a bunch of console-specific
logic, you can move it to another file and then have the console block require
that file with the -r
flag.
class Application < Rails::Application
console do
ARGV.push "-r", Rails.root.join("lib/console.rb")
end
end