【问题标题】:How do I select which attributes I want for active model serializers relationships如何为活动模型序列化程序关系选择我想要的属性
【发布时间】:2015-09-29 21:24:23
【问题描述】:

我正在使用JSONAPI formatActive Model Serializers 来创建一个带有rails-api 的api。

我有一个序列化程序,它显示了一个特定的post,它有很多topics,目前,在关系下,列出了这些主题。它目前只列出 id 和类型。我也想显示主题的标题。

有人会说在我的控制器中使用include: 'topics',但我不需要完整的主题记录,只需要它的标题。

问题:如何指定要从主题中显示的属性?

我有什么

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

我想要什么

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

我当前的序列化程序类 编辑:这就是我所追求的。

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

我尝试过的一件事 - 下面有一个答案可以使这项工作有效,但这并不是我真正想要的。

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

  belongs_to :domain
  belongs_to :user

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

【问题讨论】:

    标签: ruby-on-rails json active-model-serializers rails-api


    【解决方案1】:

    只要确保返回一个散列或散列数组,如下所示:

    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
    

    【讨论】:

    • 我赞成这个答案 b/c 它确实回答了我提出的问题之一。我不是故意混淆这个问题,但在再次遇到同样的问题后,我意识到我并不清楚。我不确定这方面的礼仪,所以请原谅我。这个问题是关于指定关系的属性,请参阅..“我想要什么”,这里的答案扩展了我尝试过的一件事。它有效,但我真正想要的是不同的。
    • 我在文档中没有看到一个很好的例子,所以我会说它不受支持或无法按照您希望的方式工作。似乎覆盖关联的函数旨在更改范围而不是属性。见这里github.com/rails-api/…。如果您不在控制器中包含或加载相关数据,那么它不会在序列化器中导致 N+1 吗?如果您使用的是 postgres,您可以从数据库返回 JSON 或在 SQL 中处理数据并使用自定义序列化程序。
    • 我让它工作了,天哪。毕竟这是正确的答案。有用。如果你有一个 has_many :topics,那么 object.topics.collect... 就可以了。如果你有一个belongs_to,你只需返回一个带有你想要的属性的散列。我必须做的一件事是记住,如果我有一个 belongs_to :user,那么我需要使用 object.user.name 来获取名称。
    【解决方案2】:

    与其定义主题方法,不如定义单独的主题序列化器,明确指定需要包含哪些属性。与定义主题方法相比,这种方法更简洁、更易于维护。

    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
    

    再次,尽量避免为关系定义方法,它不干净,也不可维护。

    【讨论】:

    • 我尝试了您推荐的相同方法,但它不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多