循环一个 ruby 散列

Loop a ruby hash

我需要像这样循环哈希:

h{ "identifier"=> ["1", "2"], "name" =>["name1", name2"] }

此散列是从表单发布的,我应该更新标识符为“1”的记录,名称为 "name1"。

那么我应该如何循环这个散列?

# for assigning "name1"  in place of "1"
h["identifier"][0] = h["name"][0]

# for assigning "name2" in place of "2"
 h["identifier"][1] = h["name"][1]

试试这个:

params[:h][:identifier].each_with_index{|key,value|
  @person=Person.find(key).update(name: params[:h][:name][value] )
}