我有以下模型:
class Task < ActiveRecord::Base
def overlapping_tasks
# call a query to return the list of overlapping_tasks of type Task based on dates
end
end
class TaskSerializer < ActiveModel::Serializer
attributes \
:name
has_many :overlapping_tasks, serializer: TaskSerializer
end
使用以下字段调用JSON API,并包含
。/tasks/1/fields[tasks]=name,overlapping_tasks,&include=overlapping_tasks
{
data:
{"id"=>"1",
"type"=>"tasks",
"attributes"=>
{
"name"=>"Task 1",
"start_date"=>"2012-01-01"
"end_date"=>"2012-12-12",
},
"relationships"=>
{"overlapping_tasks"=>
{"data"=>[{"id"=>"2", "type"=>"tasks"}]}},
"links"=>{"self"=>"/tasks/1"}}
},
"included":
{
[{"id"=>"2",
"type"=>"tasks",
"attributes"=>
{"name"=>"Task 2",
"start_date"=>"2012-01-01",
"end_date"=>"2012-12-12",
},
"relationships"=>
{"overlapping_tasks"=>
{"data"=>[{"id"=>"1", "type"=>"tasks"}]}},
"links"=>{"self"=>"/tasks/2"}}]
}
}
我遇到的问题在包含的块中,它还会返回重叠任务本身的overlay_tasks的关系。理想情况下,我只关心所获取资源的重叠,并且只想对其进行一个查询(而对于重叠部分则跳过查询)。
在fields [tasks]中,我指定对于任务类型,我们将返回overlaping_tasks,因此这将导致所包含块中的任务也返回overlaping_tasks。如果json-api规范中的资源类型与数据块相同,则json-api规范中是否有办法确定其范围?