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”)
Written by Tomasz Noworyta
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#