Ruby on rails 带有JSON Api的Rails-包含有许多关系

Ruby on rails 带有JSON Api的Rails-包含有许多关系,ruby-on-rails,include,has-many,json-api,Ruby On Rails,Include,Has Many,Json Api,我有三个模型,项目、转移和类别(又名项目类型): 这将导致JSON Api负载呈现以下形状: { data: { id, attributes: { /* the other attributes */ transfers: [ { /* full transfer object */ } ] }, relationships: { category: { data: { id, type: 'c

我有三个模型,项目、转移和类别(又名项目类型):

这将导致JSON Api负载呈现以下形状:

{
  data: {
      id,
      attributes: {
        /* the other attributes */
        transfers: [ { /* full transfer object */ } ]
      },
      relationships: {
        category: { data: { id, type: 'categories' } },
        transfers: { data: [ { /* full transfer object */ } ]
      }
    }
  },
  included: [ { type: 'category', id, attributes } ]
}
这些类别的表现符合我的预期。如何获取它,使每个
传输
都包含在
包含的
数组中,而不是嵌套在属性或关系中

谢谢大家!


编辑:不是重复的。我没有尝试嵌套响应,只是将它们包含在
included
部分以符合JSON API规范。无论如何,我找到了答案,很快就会有答案

我认为,这个问题有点重复。看看这个:


您需要使用
作为_json
和嵌套的
include

结果发现我缺少了一个
传输序列化程序
!一旦我添加了一个,它就会像你所期望的那样被放入
包含的
数组中。

尝试使用
传输
而不是
传输
。谢谢@gery,这是一个打字错误。固定的。但是仍然不起作用:/可能是重复的,我已经尝试了各种形式的呈现json:@item,include:{category:true,transfers:{include::id}},但运气不好:/
render json: @item, include: %i[transfer category]
# FWIW the include doesn't seem to affect category at all...
{
  data: {
      id,
      attributes: {
        /* the other attributes */
        transfers: [ { /* full transfer object */ } ]
      },
      relationships: {
        category: { data: { id, type: 'categories' } },
        transfers: { data: [ { /* full transfer object */ } ]
      }
    }
  },
  included: [ { type: 'category', id, attributes } ]
}