我有一些模型,其中我有以下代码:
appraisal_detail.each_value { |detail|
# some code...
}
使用消息“undefined method'pert_value'”引发NoMethodError。我做错了吗?任何建议都会受到欢迎。
以下是引发错误的方法的全部内容:
def self.update_appraisal_details(appraisal, appraisal_detail)
status = true
appraisal_detail_ids = appraisal.employee_appraisal_detail_ids
appraisal_detail.each_value { |detail|
appraisal_detail = find_by_employee_appraisal_id_and_kra_list_id(appraisal.id, detail[:kra_list_id])
if appraisal_detail.nil? # For newly added KRA
new_detail = EmployeeAppraisalDetail.new(detail.merge({:employee_appraisal_id => appraisal.id}))
status = (new_detail.save && status)
elsif appraisal_detail.status == "Inactive"
status = (appraisal_detail.update_attributes(:status => "Active") && status)
appraisal_detail_ids.delete(appraisal_detail.id)
else
appraisal_detail_ids.delete(appraisal_detail.id)
end
}
end
答案 0 :(得分:2)
您应将Hash
对象作为appraisal_detail
传递。
>> {1=>2}.each_value {|x| p x}
2
好像你传递了非哈希对象。
>> [].each_value { |x| p x }
NoMethodError: undefined method `each_value' for []:Array
from (irb):1
from /usr/bin/irb:12:in `<main>'