Jerome
Jerome

Reputation: 6189

creating multiple records with a single JSON post

The following parameters are being posted

Parameters: {
"orig_id"=>47, 
"terms_accepted"=>true, 
"email"=>"[email protected]", 
"name"=>"firstname", 
"surname"=>"surname", 
"kids"=>[
  {"school"=>"Faraway", "rate"=> "89"}, 
  {"school"=>"Transfer", "rate"=> "23"}, 
  {"school"=>"Bike", "rate"=>"4"}]
 }

However, the rails controller action, defined as follows only creates the parent record, but not the related ones:

parent = params[:parent]
@parent = Parent.new(orig_id: parent['orig_id'], terms_accepted: parent['terms_accepted'], email: parent['email'], name: parent['name'], surname: parent['surname'])
@parent.save    
kids = params[:kids]
kids each do |kid|
  @kid = Kid.new(school: kid['school'], rate: kid['rate'], parent_id: @parent.id)
  @kid.save
end

where is the syntax wrong?

Upvotes: 0

Views: 24

Answers (1)

archana
archana

Reputation: 1272

kids each do |kid| actually should be kids.each do |kid|.

Upvotes: 1

Related Questions