Today I Learned

Extend Ruby On Rails console with custom methods

If you find yourself executing complicated code to get some information in rails console and you know that you might do this many times in the futere you can consider extending Rails::ConsoleMethods.

Create a new file in config/initializers directory, for example:

config/initializers/custom_console_methods.rb

in which you can define your module and prepend it to Rails::ConsoleMethods. The most trival example would be:

module CustomConsoleMethods
  def user(id)
    User.find(id)
  end
  alias_method :u, :user
end

require 'rails/console/helpers'
Rails::ConsoleMethods.prepend(CustomConsoleMethods)