Last Updated: May 15, 2019
·
1.425K
· tomasznoworyta

Helper methods for console only

Many times we need methods that are used solely for the purpose of development or debugging. These additional lines of code should not litter the rest of application. We can easily define them as helper methods that will only be available in Rails console.

Firstly we need to create a module with helper methods and save it in the /lib directory of Rails project:

module CustomStuff
  module Console
    def u(email)
      User.where(email: email).first
    end
  end
end

Secondly we need to require that module and include it into the console in application.rb:

module MyProject
  class Application < Rails::Application
    console do
      require 'custom_stuff'
      Rails::ConsoleMethods.send :include, CustomStuff::Console
    end
  #huge portion ommitted for clarity
end

Finally in console we can write the following line:

u(“foo@foobar.com”)