Undefind local variable error

jf wrote:

I'm getting the following error when I run this line in the console:

History.find_ec_id(770893) NameError: undefined local variable or method `target_history' for History:Class

I have the following methods in my History model. I don't understand why this doesn't work. There is definitely a history with a route_id of 40 and a so_number of 770893 in the db. Any ideas on what I'm doing wrong here, or how I could do this better? Thanks!

def self.find_ec_history(so_number) histories = History.find(:all, :conditions => ['so_number = ?',so_number], :order => 'timestamp desc')

   histories.each { |his|     if his.is_ec?       target_history = his       break     end    }

    return target_history end

def is_ec?    return route_id == 40 end

target_history is local to it's original containing block. Define it ahead of time and you should be good to go.

target_history = nil histories.each { |his| if his.is_ec?    target_history = his    break end }

return target_history