How to use rails console like a boss?

yadlra profile image Yadira ・1 min read

Tips on how to use rails console like a boss! Please... I never really know how to use it and I feel this is impairing my learning.


markdown guide

One great use I have for it is as an easier way to see what a particular method inside my code is returning. Let's say I have an application helper, and it returns data in a certain way. I could put a debugger in my code, and pause the app at that point to peer inside the method. Or, alternatively, I could call the method directly inside the Rails Console and see what it is doing.


I always use the command '_', it returns the latest object from the console.
For instance sometime I might tweak an ActiveRecord query until i gets the result i am looking for then iwould just store it in a variable by doing test = _

Users.joins(:posts).where(post_id: XXX)
users = _


If there are changes in your code after rails console is loaded you can simply write reload! command to get in sync.