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

女生的网名这么多〃 提交于 2019-11-30 03:45:18

问题


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 has many topics and currently, under relationships, lists those topics. It currently only lists the id and type. I want to show the title of the topic as well.

Some would say to use include: 'topics' in my controller, but I don't need the full topic record, just its title.

Question: How do I specify which attributes I want to show from topics?

What I have

"data": {
  "id": "26",
  "type": "posts",
  "attributes": {
    "title": "Test Title 11"
  },
  "relationships": {
    "topics": {
      "data": [
        {
          "id": "1",
          "type": "topics"
        }
      ]
    }
  }
}

What I want

"data": {
  "id": "26",
  "type": "posts",
  "attributes": {
    "title": "Test Title 11"
  },
  "relationships": {
    "topics": {
      "data": [
        {
          "id": "1",
          "type": "topics",
          "title": "Topic Title"
        }
      ]
    }
  }
}

My current serializer classes EDIT: this is what I'm after please.

class PostSerializer < ActiveModel::Serializer
  attributes :title

  belongs_to :domain
  belongs_to :user

  has_many :topics, serializer: TopicSerializer

  def topics
    # THIS IS WHAT I AM REALLY ASKING FOR
  end
end

class TopicSerializer < ActiveModel::Serializer
  attributes :title, :description

  belongs_to :parent
  has_many :children
end

One thing I tried - there is an answer below that makes this work, but its not really what I'm after.

class PostSerializer < ActiveModel::Serializer
  attributes :title, :topics

  belongs_to :domain
  belongs_to :user

  def topics
    # THIS WAS ANSWERED BELOW! THANK YOU
  end
end

回答1:


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



回答2:


Instead of defining topics method, it's better to define separate topic serializer with explicitly specifying which attributes do you need to include. This is cleaner, and more maintainable approach then defining topics method.

class PostSerializer < ActiveModel::Serializer
  attributes :title

  belongs_to :domain
  belongs_to :user
  # remember to declare TopicSerializer class before you use it
  class TopicSerializer < ActiveModel::Serializer
    # explicitly tell here which attributes you need from 'topics'
    attributes :title
  end
  has_many :topics, serializer: TopicSerializer
end

Again, try to avoid defining methods for relations as much as possible, it's not clean, neither maintainable.



来源:https://stackoverflow.com/questions/32854013/how-do-i-select-which-attributes-i-want-for-active-model-serializers-relationshi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!