Moving In and Out of Debug Mode in Rails Console

I wanted to share an interesting Rails Console session with you that demonstrates how easy it is to move in and out of debug mode.  Very cool and very efficient.

We’re going to start by showing what we shouldn’t do, or rather, what we don’t need to do. We do not have to enter “–debug” on the rails console command as we might when firing up rails server –debug to trigger debug mode. I’m using the byebug debugging gem which I like with Ruby 2.0. We’ll see in a minute how we will magically trigger debug mode without the command line option.

What we want to do

What we’re after here is to view a CHECKOUT object, which in our application context is when a user finishes shopping in a store.  At that point we’re going to calculate the number and value of coupons while shopping.  I really owe you a more thorough description of the app, and I plan on doing that soon. In the meantime, without getting into too much logic or context, we’re after the CHECKOUT record with the “true” circled below, and we’re going to explore it in debug while in Rails Console.

The Model Source

We want to retrieve the ID of the CHECKOUT record with the :is_inplay = true.

Back in Rails Console

We’re going to call the User Model inplay_checkout_id method in Rails Console and create a checkout object in the process. Bam! Debug mode!

We can now play with @checkout to our heart’s content.  Automatic.

Enhanced Viewing in Debug With A Little Help From ActionView

We want to view our savings as currency. As you can see, it’s stored as a decimal data type.

Nope.  Not happening in debug. Time to jump back into IRB and get some help.

I SAID, jump back into IRB! Type “continue” to return.

That’s better. Let’s load the ActionView Helpers.

Annnnd We’re Back into Debug Mode

Snap! We’re back in debug just like that after calling our user.inplay_checkout_id method in our model. Now we can call our number_to_currency function–while in debug mode, no less–and enjoy a friendly view of our savings property.