Ruby on rails 有没有办法通过在Rails中使用Fast JSON API来选择外部对象的一个属性?

Ruby on rails 有没有办法通过在Rails中使用Fast JSON API来选择外部对象的一个属性?,ruby-on-rails,json,api,fastjsonapi,Ruby On Rails,Json,Api,Fastjsonapi,我正在创建一个使用Rails后端API的旅游应用程序。我决定使用Fast JSON API来序列化我的数据。我有一份国家名单;每个国家都有许多城市,每个城市都有许多景点 这些是我的模型之间的关联列表 Country.rb has_many :cities has_many :attractions, through: :locality Localities.rb has_many :attractions belongs_to :country Attraction.rb belongs

我正在创建一个使用Rails后端API的旅游应用程序。我决定使用Fast JSON API来序列化我的数据。我有一份国家名单;每个国家都有许多城市,每个城市都有许多景点

这些是我的模型之间的关联列表

Country.rb 
has_many :cities
has_many :attractions, through: :locality

Localities.rb
has_many :attractions
belongs_to :country

Attraction.rb
belongs_to :locality
当我为单个景点序列化数据时,我只希望包括城市的名称属性和它所属国家的名称属性。我目前正在通过添加可选参数来实现这一点,以包括地区和国家名称

  def show
    attraction = Attraction.find_by(slug: params[:slug])
    options = {}
    options[:include] = [:locality, :'locality.country.name']
    render json: AttractionSerializer.new(attraction, options).serialized_json
  end
但是,这将提供国家的所有属性和关系,包括国家内嵌套的所有不相关地区的列表,当我的数据集变得更大时,这将变得非常低效。见下文:

{
"data": {
    "id": "1",
    "type": "attraction",
    "attributes": {
        "name": "Plaza de España",
        "description": "Site of the World Exposition in 1929",
        "types": null,
        "latitude": 40.4232824,
        "longitude": -3.7107257,
        "slug": "plaza-de-espana",
        "locality": {
            "id": 1,
            "name": "Seville",
            "country_id": 168,
            "created_at": "2020-06-10T05:43:47.474Z",
            "updated_at": "2020-06-10T05:43:47.474Z",
            "slug": "seville",
            "latitude": 37.3886303,
            "longitude": -5.9953403
        }
    },
    "relationships": {
        "locality": {
            "data": {
                "id": "1",
                "type": "locality"
            }
        }
    }
},
"included": [
    {
        "id": "1",
        "type": "locality",
        "attributes": {
            "name": "Seville",
            "latitude": 37.3886303,
            "longitude": -5.9953403,
            "slug": "seville"
        },
        "relationships": {
            "country": {
                "data": {
                    "id": "168",
                    "type": "country"
                }
            }
        }
    },
    {
        "id": "168",
        "type": "country",
        "attributes": {
            "name": "Spain",
            "slug": "spain",
            "iso_3166_1_alpha2": "ES",
            "iso_3166_1_alpha3": "ESP",
            "iso_3166_1_numeric": "724"
        },
        "relationships": {
            "localities": {
                "data": [
                    {
                        "id": "1",
                        "type": "locality"
                    },
                    {
                        "id": "2",
                        "type": "locality"
                    },
                    {
                        "id": "3",
                        "type": "locality"
                    },
                    {
                        "id": "4",
                        "type": "locality"
                    },
                    {
                        "id": "5",
                        "type": "locality"
                    },
                    {
                        "id": "6",
                        "type": "locality"
                    }
                ]
            },
            "attractions": {
                "data": [
                    {
                        "id": "1",
                        "type": "attraction"
                    }
                ]
            }
        }
    }
]
}

有没有办法在JSON中只包含一个属性(即国家名称),而不是整个对象?(景点嵌套在国家/地区下方两层)


非常感谢。

您需要创建多个序列化程序,这些序列化程序协同工作以实现此目的

class AttractionSerializer
      include FastJsonapi::ObjectSerializer

      attributes :attraction_attr_1, :attraction_attr_2, etc.

      belongs_to :locality, serializer: AttractionLocalitySerializer
end
class AttractionLocalitySerializer
      include FastJsonapi::ObjectSerializer

      attributes :name

      belongs_to :country, serializer: AttractionCountrySerializer
end
class AttractionCountrySerializer
      include FastJsonapi::ObjectSerializer

      attributes :name
end