DEV Community

Yadira
Yadira

Posted on

How to use rails console like a boss?

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.

Top comments (3)

Collapse
 
bengreenberg profile image
Ben Greenberg

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.

Collapse
 
romainverbeke profile image
Romain Verbeke • Edited

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 = _

Collapse
 
bilalbudhani profile image
Bilal Budhani 👨‍💻

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