How do I select which attributes I want for active model serializers relationships

后端 未结 2 1232
梦毁少年i
梦毁少年i 2021-01-05 04:46

I am using the JSONAPI format along with Active Model Serializers to create an api with rails-api.

I have a serializer which shows a specific post that

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 05:38

    Just make sure to return a hash or array of hashes like so:

    def videos
        object.listing_videos.collect do |lv|
          {
            id: lv.video.id,
            name: lv.video.name,
            wistia_id: lv.video.wistia_id,
            duration: lv.video.duration,
            wistia_hashed_id: lv.video.wistia_hashed_id,
            description: lv.video.description,
            thumbnail: lv.video.thumbnail
          }
        end
      end
    

提交回复
热议问题