我有一个 rails api,其中包含许多由 fast_jsonapi gem 序列化的模型。
这是我的模型的样子:
class Shift < ApplicationRecord
belongs_to :team, optional: true
...
class Team < ApplicationRecord
has_many :shifts
...
这就是序列化器的样子
class ShiftSerializer
include FastJsonapi::ObjectSerializer
...
belongs_to :team
...
end
序列化工作。但是,即使我包含了复合团队文档:
def index
shifts = policy_scope(Shift).includes(:team)
options = {}
options[:include] = [:team, :'team.name', :'team.color']
render json: ShiftSerializer.new(shifts, options)
end
我仍然得到像这样格式化的对象:
...
relationships: {
team: {
data: {
id: "22",
type: "Team"
}
}
}
而我也期望获得我的团队模型的属性。