corodio8
corodio8

Reputation: 21

How to get variable states in Rails?

This is kind of a general question that I've hit several times. I'm writing validators or something in my model and then I run some tests in rspec and one of my conditionals like this:

validate :some_validator    

def some_validator
  if some_attribute.count == 1         <-- Here!
    run_this_method
  end
end

that evaluates true, and I have no idea why. Usually i'm able to backtrace the code and figure it out why, but even then i'm usually wasting hours tracking down one or two things.

So is there a way to just have rails stop and tell me whats going on at whatever line?

Upvotes: 0

Views: 26

Answers (1)

Powers
Powers

Reputation: 19308

Pry is a good Ruby/Rails debugger for stopping a program while it is executing and inspecting the variables.

binding.pry adds the breakpoint and you can play with the code in your Terminal.

def some_validator
  binding.pry
  if some_attribute.count == 1         <-- Here!
    run_this_method
  end
end

Here are some useful learning resources for Pry:

  1. RailsCast on Pry
  2. pry-Rails gem

Upvotes: 1

Related Questions