我认为这个答案在下面
所以说我有类似JSON POST的内容:
{
"contact":{
"first_name": "Bill",
"last_name": "Clinton",
"phone_numbers":[
{
"name": "blah",
"number": "555-555-5555"
},
{
"name": "blah2",
"number": "555-555-5555"
}
]
}
}
请原谅我对JSON的糟糕格式。 无论如何,我想把它保存到我的数据库中。 Rails控制器将负责这一点。现在 我有点累,但我不确定如何处理这个,因为有一个嵌套数组。 请帮忙。
所以我有一个联系人表,其中有一个phone_number_id字段。 phone_number表有名称和号码字段
当前尝试直到我意识到我有一个嵌套数组:
@phone_number = Phone_Number.new
@phone_number.contact_id = @contact.id
@phone_number.name = @params[:phone_number_name]
答案 0 :(得分:0)
这是我提出的解决方案
if @contact.save
@params[:phone_numbers].each do |counter|
@phone = PhoneNumber.new
@phone.contact_id = @contact.id
@phone.name = counter[:name]
@phone.number = counter[:number]
@phone.save
end