【发布时间】:2018-01-17 17:27:42
【问题描述】:
我正在尝试获取一个看起来像这样的 JSON
{
"data" : {
"type": "field_definition",
"id": 5,
"attributes": {
"specifier": "foo",
"entity_type": "bar"
}
"relationships": {
"active_collections" : [1,2,3]
}
}
}
我做了一个测试类,我用它作为我的模型:
class Test
attr_reader :id,
:specifier,
:entity_type,
:active_collections
def initialize
@id = 5
@specifier = "foo"
@entity_type = "bar"
@active_collections = [1,2,3]
end
end
我的序列化器:
class SerializableFieldCollection < JSONAPI::Serializable::Resource
type 'field_collection'
attributes :specifier, :entity_type
has_many :active_collections
end
我把所有东西都称为
def index
render jsonapi: Test.new,
class: SerializableFieldCollection,
status: 200
end
{"data"=>
{"id"=>"5", "type"=>"field_collection", "attributes"=>{"specifier"=>"foo", "entity_type"=>"bar"}, "relationships"=>{"active_collections"=>{"meta"=>{"included"=>false}}}}}
有人能指出正确的方向吗?如何使用 jsonapi-rb gem 中的关系/has_many 函数?
【问题讨论】: