我有一个包含以下类的简单模型:
class Country < ApplicationRecord
has_many :country_categories
has_many :categories, through: :country_categories
end
class CountryCategory < ApplicationRecord
belongs_to :country
belongs_to :category
end
class Category < ApplicationRecord
has_many :country_categories
has_many :countries, through: :country_categories
end
和资源:
class CountryResource < JSONAPI::Resource
has_many :country_categories
end
当我对响应进行GET
请求时/countries/1?include=country_categories.category
,总是会带来很多链接。例如
{
"data": {
"id": "31",
"type": "countries",
"attributes": {
"description": null
},
"relationships": {
"country-categories": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/countries/31/relationships/country-categories",
"related": "http://api.localhost.local:3000/api/v1/countries/31/country-categories"
},
"data": [
{
"type": "country-categories",
"id": "129"
},
{
"type": "country-categories",
"id": "130"
}
]
},
}
},
"included": [
{
"id": "129",
"type": "country-categories",
"attributes": {
"stuff": "aaa"
},
"relationships": {
"country-subcategories": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/country-categories/129/relationships/country-subcategories",
"related": "http://api.localhost.local:3000/api/v1/country-categories/129/country-subcategories"
}
},
"category": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/country-categories/129/relationships/category",
"related": "http://api.localhost.local:3000/api/v1/country-categories/129/category"
},
"data": {
"type": "categories",
"id": "1"
}
}
}
},
{
"id": "130",
"type": "country-categories",
"attributes": {
"stuff": "sfasf"
},
"relationships": {
"country-subcategories": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/country-categories/130/relationships/country-subcategories",
"related": "http://api.localhost.local:3000/api/v1/country-categories/130/country-subcategories"
}
},
"category": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/country-categories/130/relationships/category",
"related": "http://api.localhost.local:3000/api/v1/country-categories/130/category"
},
"data": {
"type": "categories",
"id": "3"
}
}
}
},
{
"id": "1",
"type": "categories",
"links": {
"self": "http://api.localhost.local:3000/api/v1/categories/1"
},
"attributes": {
"name": "Value extraction"
},
"relationships": {
"subcategories": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/categories/1/relationships/subcategories",
"related": "http://api.localhost.local:3000/api/v1/categories/1/subcategories"
}
}
}
},
{
"id": "3",
"type": "categories",
"links": {
"self": "http://api.localhost.local:3000/api/v1/categories/3"
},
"attributes": {
"name": "Enabling environment"
},
"relationships": {
"subcategories": {
"links": {
"self": "http://api.localhost.local:3000/api/v1/categories/3/relationships/subcategories",
"related": "http://api.localhost.local:3000/api/v1/categories/3/subcategories"
}
}
}
}
]
}
所有这些links
都不会被使用。有没有办法可以删除它们?谢谢 :)