我想要像{年龄: 20,编号:2},{年龄: 21,编号: 1},...这样的age_list数据。
但是我在保存json数组时遇到了问题。
现在,
# age_list's db settings
def change
add_column :customers, :age_list, :json, null: true
end# get params
def customers_params
params.require(:customers).permit(:male, :female, age_list:[:age, :num])
end# saved age_list data in the database column
{
"0": {
"age": "20",
"num": "1"
},
"1": {
"age": "21",
"num": "1"
}
}怎样才能像我想要的那样保存json数组?谢谢。
发布于 2020-12-15 12:32:58
我试过了
# get params
def customers_params
params.require(:customers).permit(:male, :female, age_list:[:age, :num]).tap do |whitelisted|
whitelisted[:age_list] = params[:customer_group][:age_list].present? ? params[:customer_group][:age_list].values : null
end然后我可以将其另存为下面的json数组
[
{
"age": "20",
"num": "1"
},
{
"age": "21",
"num": "1"
}
]
or
nullhttps://stackoverflow.com/questions/65246598
复制相似问题