Sachin Prasad
Sachin Prasad

Reputation: 5411

Issue with saving json data received in rails

Here is my json data:

{
   "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"
         }
      }
   }
}

I need to loop and save name,DOB and charge.Please not 0,1 is not fixed,their size can be upto any number.

I'm trying this :

     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

But the data saved is null.Please help me.

Upvotes: 0

Views: 39

Answers (1)

Rahul Tapali
Rahul Tapali

Reputation: 10137

You can try this:

 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

No need of counter

Upvotes: 1

Related Questions