How to run code automatically when launching a Rails console?
Asked Answered
L

2

10

Let's say I wanted a greeting every time the Rails console comes up:

Scotts-MBP-4:ucode scott$ rails c
Loading development environment (Rails 4.2.1)
Hello there! I'm a custom greeting
2.1.5 :001 >

Where would I put the puts 'Hello there! I\'m a custom greeting' statement?

Another Stackoverflow answer suggested, and I've read this elsewhere too, that I can put that in an initializer like this:

# config/initializers/console_greeting.rb
if defined?(Rails::Console)
  puts 'Hello there! I\'m a custom greeting'
end

That doesn't work for me though :(. Even without the if defined?(Rails::Console) I still don't get output. Seems like initializers are not run when I enter a console, despite what others suggest.

Lithograph answered 16/4, 2015 at 21:54 Comment(4)
I routinely put them exactly where you indicate, without the if defined?(Rails::Console), and have never had an issue... I wonder if there is something else at play, here?Testudinal
The "Hello there...." line does get displayed when I run the server with rails s. So the initializer is getting executed. Just not when launching a console. Only when launching the whole server.Lithograph
bwerth@worth:~/rails/my_site$ bundle exec rails c "Devise monkey patch for v 3.4.1. Check before moving on" Loading development environment (Rails 4.0.2)Testudinal
It might be worth mentioning that p ('Devise monkey patch for v 3.4.1. Check before moving on') is the first line in config/initializers/devise_minkey_patch.rb... What other gems are you using? Any logger shenanigains?Testudinal
S
6

The following will work in Rails 6:

Just pass a block to Rails.application.console, e.g

    # config/initializers/custom_console_message.rb
    if Rails.env.production?
      Rails.application.console do
        puts "Custom message here"
      end
    end

Now when starting the rails production console, the custom message will be printed. This code will not be executed when you start rails server.

Remove the if Rails.env.production? if you want this to run in all environments.

Spanos answered 23/1, 2021 at 14:25 Comment(2)
Thanks for your answer. Do you know if there is a file location - kinda like the /tests folder - that guaranteed will not be loaded in a production db? I do note that you have a conditional Rails.env.production? but was curious if there was a different way to handle it?Swane
Not a file location, no. I still think this is quite a clean solution - to avoid use in prod you could just switch it to unless Rails.env.production? or whatever.Spanos
G
4

I use ~/.irbrc for similar purposes (I require a gem in each console session). For example, my .irbrc

if (defined? Rails)
  # Rails specific
end

# common for all irb sessions

You could use your project name to limit executing code to only one project's console:

if (defined? Rails) && (defined? YourProject)
  # code goes here
end
Gignac answered 16/4, 2015 at 22:20 Comment(1)
Is there a way to make this work with Heroku as well?Lithograph

© 2022 - 2024 — McMap. All rights reserved.