Undefind local variable error

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

Whoops! I meant I ran the following in the console:

History.find_ec_history(770893)

jf wrote:

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


http://www.5valleys.com/

You’re right!

Thanks so much for your help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs