从rails中的活动记录方法(简化以说明流程):
class Model
include Mongoid:Document
include Mongoid:Timestamps
belongs_to :record
belongs_to :structure
has_one :sim_engine, dependent: :destroy
def init #Line 22
if new_record?
self.structure = record.structure
self.sim_engine = SimEngine.create(function1,other_inputs...)
end
end
def function1
input_1 = ...
input_2 = ...
function1_hash = [ ]
...
m = function2(input_1,input_2)
function1_hash += m
rescue Exception => e
throw "Error in record #{record.id}: #{e.message}" #Line 683
end
def function2(input_1,input_2)
input_1.each do |value|
function2_hash << value
end
temp = input_2.anotherClassFunctionCall()
function2_hash << temp
end
我跑的时候:
Model.create!(record: 314675)
我收到以下错误跟踪:
ArgumentError: uncaught throw "Error in record 314675: undefined method '[]' for nil:NilClass'
from org/jruby/RubyKernel.java:1311: in 'throw'
from /srv/app/models/model.rb:683: in 'function1' # See above sample code
from /srv/app/models/model.rb:22: in 'init' # See above sample code
# The line numbers are commented in the above sample code
我试图找出错误异常被抛出的原因,这是我的假设:
function1
方法中存在需要解决的错误。function2
继承的function1
方法出错。答案 0 :(得分:0)
我找出了
的原因ArgumentError:uncaught throw“记录314675中的错误:nil的未定义方法'[]':NilClass'
主要来自 def init 方法所需的一组不完整的输入变量。因为这是从 function1 方法抛出的 ArgumentError ,所以我只投资于调试 function1 。原始模型包含7个输入变量,包括 function1 。我意识到几个输入变量与数据类型不匹配,例如,模型采用整数,而输入变量采用字符串。