maurice
maurice

Reputation: 329

Does variable scope change when using binding.pry in rails?

In a controller action, I need to check whether the controller is being accessed by the application itself (which would set a session variable current_user), or from an api call. The beginning of my controller looks like this:

   130:   def create
   131:     # handle the possibility that this is an api call
   132:     api_call = current_user.nil?
=> 133:     binding.pry

When I pry in to see what is going on, I get this nonsensical output:

[1] pry(#<Crm::ConnectionsController>)> api_call
=> false
[2] pry(#<Crm::ConnectionsController>)> current_user
=> nil
[3] pry(#<Crm::ConnectionsController>)> current_user.nil?
=> true
[4] pry(#<Crm::ConnectionsController>)> api_call = current_user.nil?
=> true
[5] pry(#<Crm::ConnectionsController>)> api_call
=> true

And when I change the variable api_call from the pry console with the following line, then resume execution, the controller behaves from there on as if api_call is set to true.

Why would this be? Why is my controller not setting api_call correctly in the first place?

Upvotes: 0

Views: 485

Answers (1)

Conrad Irwin
Conrad Irwin

Reputation: 1312

This shouldn't happen at all (I'm one of the Pry developers). The most likely scenario is that current_user is returning a non-nil value the first time you call it, but caching nil to return for all future calls.

If you've double checked that, and you're still having the problem, I'd need to see more of your code to debug it.

Upvotes: 2

Related Questions