Params being made nil inexplicably

Summary of problem: my params hash is being made nil in my controller without any reason that I can identify.

Everything was fine until I put this new code as the first lines of code in my controller. But the block is not being executed because the puts statement is not evaluated:

  def calculate

      if params[:id]         puts "in if statement"         @query = Query.where(:id => params[:id])         params = eval(@query[0].queryString).to_options         params[:query_end_date].to_options!         params[:query_start_date].to_options!       end

The controller then fails in the first line of the next block of code (below), and the error is that I am evaluating a nil object.

      if params[:entity_selection]=="ELB"         @instances_array = get_instances_array(params[:instance])         @grouped_elb_instances = get_grouped_elb_instances(@instances_array, params[:instance])       else         @instances_array =         @grouped_elb_instances =       end

If I comment out the first 'if' block, the controller works perfectly. When I print the params before the if statement, it has a value, but for some reason, it seems like the params variable is cleared from memory. I have no idea what is happening.

Any ideas?

Are you *sure* the "if" is not getting run through? Change the "puts" to a "raise"...

Where are you looking for the output of the puts? On the web page? In the log? On the screen of the server? In too many faces? :wink:

-Dave

@Michael: I'll give this a shot soon and let you know what happens.

@Dave: I was looking for the output on the screen of the server. Should I look somewhere else?

I'd have to post the entire controller to show you this, but when I included my begin-rescue-ensure block (with the criminal if statement at the top of the begin block), the error still happens, but not until the ensure block..even though the params hash is referenced multiple times before then.

I can post the whole controller if that would be helpful, but in any case, I'll see what happens with a raise statement in the criminal if.

Let me know if you have any other suggestions.

JS

Michael Pavling wrote in post #1022225:

Summary of problem: my params hash is being made nil in my controller without any reason that I can identify.

Everything was fine until I put this new code as the first lines of code in my controller. But the block is not being executed because the puts statement is not evaluated:

def calculate

  if params\[:id\]
    puts "in if statement"
    @query = Query\.where\(:id => params\[:id\]\)
    params = eval\(@query\[0\]\.queryString\)\.to\_options
    params\[:query\_end\_date\]\.to\_options\!
    params\[:query\_start\_date\]\.to\_options\!
  end

Local variables are tricky - merely mentioning them causes them to spring into existence. For example if you run

if false   x = 2 end

Then the local variable x is created, with value nil.

I think the same thing is happening here - a local variable called params is being created, which is shadowing the params instance method.

Fred

@Fred,

Thanks man. I agree, that could be the problem. However, to test it out, I commented out everything inside the if part of the 'criminal if-else' and no exception is raised. For example, the new code that works is:

def calculate

      #Check if request is from a linked URL or direct request from site       puts "before if statement"       puts params[:entity_selection]

      if params[:id]         #puts "in if statement"         #@query = Query.where(:id => params[:id])         #params = eval(@query[0].queryString).to_options         #params[:query_end_date].to_options!         #params[:query_start_date].to_options!       else         puts "in else"         puts params[:entity_selection]       end

So if it were the case that the params hash being mentioned could overwrite it, then this version of the code would fail, right?

I appreciate the help. I'll continue to try to figure this out. If this doesn't work, it could be anarchy out there. I'm worried.

@Fred,

Thanks man. I agree, that could be the problem. However, to test it out, I commented out everything inside the if part of the 'criminal if-else' and no exception is raised. For example, the new code that works is:

def calculate

  \#Check if request is from a linked URL or direct request from site
  puts "before if statement"
  puts params\[:entity\_selection\]

  if params\[:id\]
    \#puts "in if statement"
    \#@query = Query\.where\(:id => params\[:id\]\)
    \#params = eval\(@query\[0\]\.queryString\)\.to\_options
    \#params\[:query\_end\_date\]\.to\_options\!
    \#params\[:query\_start\_date\]\.to\_options\!
  else
    puts "in else"
    puts params\[:entity\_selection\]
  end

So if it were the case that the params hash being mentioned could overwrite it, then this version of the code would fail, right?

Except that it's not mentioned any more - it's commented out ( mentioned is obviously a rather wooly term)

Fred

I see what you mean. However,

x=3 if false    x=2 end

puts x

This code outputs 3, not nil. And I think that's a more appropriate analogy. I really don't understand what's happening though.

Frederick Cheung wrote in post #1022322:

return 3 is the right! the value dont change and never in into if, because you put false…its right

@leoncio:

I was responding to Fred's suggestion that params was being overwritten because it was mentioned in the if statement in the original code. Forget my replies to Fred, do you have any idea why the params variable is being made nil in the original post?

leoncio caminha wrote in post #1022336:

Use ruby-debug to break into the code and see what is happening. See the Rails Guide on debugging if you do not know how to do that.

Colin

I see what you mean. However,

x=3 if false x=2 end

An even better example is

puts xyz # should raise name error

if false   xyz = 1 end

puts xyz #=> nil

The thing is that params isn't initially a local variable - it's a method that then gets shadowed by a local variable, i.e.

def xyz   "moo" end

puts xyz.inspect #=> 'moo' if false   xyz = 1 end puts xyz.inspect #=> nil

Fred

Unrelated, but in the name of all that is holy don't do this. Look into the 'serialize' method:

for a cleaner, faster, safer way to stash whole data structures in the DB.

--Matt Jones

PS: Also, Query.find(params[:id]) is a good bit more understandable than doing a where and then getting the first element.

@leoncio:

I was responding to Fred's suggestion that params was being overwritten because it was mentioned in the if statement in the original code. Forget my replies to Fred, do you have any idea why the params variable is being made nil in the original post?

I think perhaps you are thinking that a local variable created inside an if block will disappear at the end of the block, so that the original variable is restored. Wrong.

Colin

@Matt,

Serializing is definitely the right way to go. I'll get into that. And of course, I'll use the find method on the query.

Awesome advice. Very appreciative.

Matt Jones wrote in post #1022343:

@Fred, Matt:

Awesome example and good explanation. I'll have to dig into this issue as well.

Thanks to everyone for the help! Amazing group.