Today I Learned

hashrocket A Hashrocket project

Setup Your Rails Console with .pryrc

Sometimes there are variables I want to initialize or setup I want to run each time I enter a rails console, and I don't want to have to type it out every time. I'm using pry-rails, so my rails console is a pry session, and I can add ruby code to my .pryrc file to be run on console startup. Conveniently, this runs after the environment is loaded, so I have access to all my ActiveRecord models.

# .pryrc
author = Author.find(1)

If I put the above in my .pryrc, then I've got author available to me whenever I'm in a rails console (but not when I drop into a pry session in rails server (which is fine, I think it's less useful there)).

You can use a local .pryrc in your project's directory, or a global config in ~/.pryrc.

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.