这是我的json数据:
{
"authenticity_token" =>"pdXG/h4Ds0g2ysC564mNSfGe7HfpPWHD+X81u06hJFI=",
"utf8" =>"✓",
"ar_identifier" =>"14433",
"bji" => {
"date" =>"02-12-2013",
"ar_bji_o_involveds" => {
"0" => {
"name" =>"h",
"dob" =>"",
"charge" =>"j"
},
"1" => {
"name" =>"jkljlkj",
"dob" =>"",
"charge" =>"jkljklj"
}
}
}
}
我需要循环并保存名称、DOB和费用。请不要0,1不是固定的,它们的大小可以是任何数字。
我正在尝试这个:
counter = 0;
other = params[:bji][:ar_bji_o_involveds]
other.each do |other|
e = MyModel.new(params[:bji][:ar_bji_o_involveds][:counter])
e.save
counter+= 1
end
但保存的数据为空。请帮帮我。
你可以试试这个:
other = params[:bji][:ar_bji_o_involveds]
other.each do |key, value|
e = MyModel.new(value) #Assuming your model has `dob`, `charge` and `name` fields
e.save!
end
无需counter