2013-02-11 115 views
0

這裏收到的JSON數據是我的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" 
     } 
     } 
    } 
} 

我需要循環和保存的名字,出生日期和charge.Please不是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 

但保存的數據是null.Please幫助我。

回答

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

+0

非常感謝快速reply.This是問題:) – 2013-02-11 16:18:12