How to Patch the Console?

Hi everbody,

I am working on a Rails Project using OrientDB as Database ( )

This gem initializes any available database-class and assigns it to model-classes.

For me its convenient to recognize, which database-classes are found, when starting the console.

Thus I patched the source in railties/rails/lib/commands/console.rb

Obviously, I am not happy with this dirty approach.

How is this done properly?

in detail:

I want to execute some ruby-code after any initialization of the core and before the first command can be entered.

This code prints out some status information.

Any help is appreciated

greetings from Stuttgart


Hi Hartmut,

My suggestion would be to add your code in the initialisers folder, something like config/initializers/console.rb and this will run after Rails has initialised its environment.

However this will run every time you run a rails or rake command, so to avoid that you could add something like this to your console initialiser:

if Rails.const_defined?(“Console”)

your code here


And this will run only if you are on a Rails console.

When I tried the code myself I had some problems with the binaries generated by spring, so you might need to update them if it doesn’t work. rake rails:update:bin or rails app:update:bin if you’re on Rails 5.

Hope it works.

Kind regards,

/ Marco

In application.rb you can put

console do

#only executed when booting console


We use this to inject some helper methods into console sessions


This works perfectly if inserted in the application-block.

Thanks alot.

I added your Input to the documentation in