Tuesday, November 21, 2017

How to debug a Rails runner script in Jetbrains RubyMine

For anyone using an IDE debugging and stepping through a script is generally a trivial affair. But what if your script isn't directly run by the interpreter?

Right now I'm working on a ruby script that needs to get access to the resources I built in my rails app; specifically, I want to use the activerecord model AND the rails database connector. The solution was to write a ruby script that was called by rails runner.

I had an issue where during my testing, I thought there were some puts calls that were being skipped for some unknown reason, and I wanted to debug and step through the area in question. The most popular solution on Google and SO was to use puts statements followed by a ton of bundler.pry calls. Compared to other languages (where I could step in, over, and out of code blocks), this was very cumbersome. I was about to go down this path, when...

Someone asked if I had used the Jetbrains Rubymine debugger. I did try it but it didn't seem obvious as to how I could configure it to handle my situation. Then JB was nice enough to post this video and voila! Now because I'm one of the few schlubs on this planet that write my rails app on Windows, I had to spend another couple of minutes figuring out some additional things but I got it working!

Below is a screenshot of my working debug config.