Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Are you tired of typing always the same things on Rails console? what if you don’t need anymore?

Posted on 2018-07-28 by Rodrigo Urubatan

After you spend some time working in the same Rails project, being this project whatever project you are working for some time now, you probably use the Rails console a lot, many reasons:

  • Testing code
  • Seting up data
  • Using “binding.pry” to “debug” some controller or model
  • Put whatever you are doing in “rails c” now here

And doing it every day, there are tons of things you write there every day, and some times for security reasons your code starts to get more complicated, for example, in one of my projects, the username is encrypted, and I need to call “encrypt_username” before passing the parameter to “find_by”.

For example, in this project, every time I wanted to lookup a user, I had to write:

User.find_by(username: User.encrypt_username(name))

And as a lazy person that I am, I decided that there should be one easier way to do it, and after some lookup I found out that I could add methods do a “~/.irbrc” file, but that was not good enough, because I work with many projects, and those projects have different code, and many of the “helper methods” would work for one and not for the other.

So I kept digging, and found out that the awesome Rails team had already solved this problem for me, I just created an initializer in the file “config/initializers/console_methods.rb” and added the following code:

Rails.application.console do
  def find_user(name)
    User.find_by(username: User.encrypt_username(name))
  end
end

Now, every time I open the console in that project, I just type “find_user(‘my name’)” and my user is returned.

Of course this is kinda “hackish” solution, the best organization for this, would be to add all the methods I want in the console in a module, and then use the initializer to just include that module, for exemplo creating a “lib/console_utils.rb” with this code:

  # These methods are included into the Rails console
  module ConsoleUtils
    # Find a user by username
    def find_user(username)
      User.find_by(username: User.encrypt_username(username))
    end
  end

then use that initializer we’ve created before to include the module:

Rails.application.console do
  require 'console_utils'
  Rails::ConsoleMethods.send :include, ConsoleUtils
end

Now, you just need to add the methods to scratch your own itch to the module in “lib/console_utils.rb” and be a lot happier with the tools that Rails already provides!

For example, in another project, I added a method to create easily a JWT token to send requests to a service that shares this authentication (wanna know more about using JWT with Rails?).

In another, a method to save a CSV report from some data that I use a lot;

Ok, but how do you use these methods?

Just open your rails console and type:

find_user('my name')

Now, if you have questions about these tips, or have more ideas of useful console methods, please leave a comment bellow!

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme