0

这是我的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

但是保存的数据是空的。请帮帮我。

4

1 回答 1

1

你可以试试这个:

 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

于 2013-02-11T16:12:24.340 回答