我正在尝试使用JSON API 规范在 Rails 中构建 API
我的用户模型具有与之关联的更改对象。例如,用户可以进行以下更改:
{
id: 10,
type: 'changes',
diffs: [
{
subject: 'email',
from: 'old@example.org',
to: 'new@example.org'
},
{
subject: 'tags',
from: []
to: [{ id: 1, type: 'tags', label: 'Ruby' }]
}
]
}
如您所见,该diffs
属性包含一组更改差异,第二个差异是一组对象差异。我想修改这种格式(如果可能的话),使其符合 JSON API 规范。像这样:
{
subject: 'tags',
from: []
to: [{ id: 1, type: 'tags' }]
}
并将标签的属性放入包含的部分:
included: [
{
id: 1,
type: 'tags',
attributes: { label: 'Ruby' }
}
]
问题:差异是一个对象数组(不是记录,它们没有 ID),其中包含带有记录的字段。是否可以将响应格式化为如此深嵌套的记录(如我的情况下的标签)将引用包含部分中的记录?
我想使用fast_jsonapi gem