我試圖得到一個JSON它看起來像這樣jsonapi-RB關係實例
{
"data" : {
"type": "field_definition",
"id": 5,
"attributes": {
"specifier": "foo",
"entity_type": "bar"
}
"relationships": {
"active_collections" : [1,2,3]
}
}
}
我做了哪些im使用作爲我的模型測試類:
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函數的正確方向嗎?
我想'JSONAPI :: Serializable接口:: Resource'都有自己impelementation不涉及'ActiveRecord'?根據'jsonapi-rails'提供的文檔'has_many'是正確的使用 – Rhs
鏈接文檔http://jsonapi-rb.org/guides/serialization/defining.html – Rhs
'has_many'確實是'JSONAPI :: Serializable :: Resource' DSL。 – beauby