【发布时间】:2018-05-16 12:04:42
【问题描述】:
我正在使用祖先 gem 创建一个嵌套菜单表。
当我render json: Menu.all
我明白了:
[
{
"id": 1,
"label": "Menu"
"created_at": "2018-05-14T14:28:03.883Z",
"updated_at": "2018-05-14T14:28:03.883Z",
"ancestry": null
},
{
"id": 2,
"label": "Menu 1-1",
"created_at": "2018-05-14T14:28:13.982Z",
"updated_at": "2018-05-14T14:28:13.982Z",
"ancestry": "1"
},
{
"id": 3,
"label": "Menu 1-1-1",
"created_at": "2018-05-14T14:28:13.982Z",
"updated_at": "2018-05-14T14:28:13.982Z",
"ancestry": "1/2"
},
etc
我想做的也是返回父母的 id(我很欣赏它是祖先字段中的最后一个数字,但我希望它分开。
所以我得到:
[
{
"id": 1,
"label": "Menu"
"created_at": "2018-05-14T14:28:03.883Z",
"updated_at": "2018-05-14T14:28:03.883Z",
"ancestry": null,
"parent_id":null
},
{
"id": 2,
"label": "Menu 1-1",
"created_at": "2018-05-14T14:28:13.982Z",
"updated_at": "2018-05-14T14:28:13.982Z",
"ancestry": "1",
"parent_id":"1"
},
{
"id": 3,
"label": "Menu 1-1-1",
"created_at": "2018-05-14T14:28:13.982Z",
"updated_at": "2018-05-14T14:28:13.982Z",
"ancestry": "1/2",
"parent_id":"2"
},
有人知道如何实现吗?
【问题讨论】:
标签: ruby-on-rails json api ancestry