1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Add Run Some Code When Rails Console Starts as a Rails til

This commit is contained in:
jbranchaud
2022-05-10 12:59:24 -05:00
parent ded9121f35
commit 3608973f74
2 changed files with 38 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
# Run Some Code Whenever Rails Console Starts
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:
```ruby
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.
```ruby
class Application < Rails::Application
console do
ARGV.push "-r", Rails.root.join("lib/console.rb")
end
end
```
[source](https://til.hashrocket.com/posts/avb2v3ubdt-pass-a-block-on-console-load)