-1

我有一些模型,其中包含以下代码:

appraisal_detail.each_value { |detail|
  # some code...
}

它使用消息“未定义的方法'each_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
4

1 回答 1

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>'
于 2013-08-29T07:28:32.843 回答