简体   繁体   English

将ActiveModel :: Serializer从v0.9.2迁移到0.8,has_many没有被序列化

[英]Migrating ActiveModel::Serializer down from v0.9.2 to 0.8, has_many not being serialised

I have the following ActiveModel::Serializer class 我有以下ActiveModel::Serializer

class MyThingySerializer < ActiveModel::Serializer
  root false

  attributes :id, :name, :description

  has_many :whatsits, embed_namespace: :_embedded

  delegate :whatsits, to: :object

end

It was working fine under AMS 0.9.2 but, in order to add an optional attribute using the include_attributename? 在AMS 0.9.2下运行良好,但是为了使用include_attributename?添加可选属性include_attributename? mechanism I was told to roll-back to AMS 0.8 被告知要回滚到AMS 0.8的机制

Now my whatsits don't appear in my serialised output under the _embedded attribute. 现在,我的whatsits不会出现在_embedded属性下的序列化输出中。

Is there something special I need to do to get my embedded whatsits back? 我需要做些特别的事情来找回嵌入的whatsits吗?

Update 更新资料

I've tried adding the following method to my Serializer: 我尝试将以下方法添加到我的序列化器中:

def whatsits
  associated = self.class._associations[:whatsits]
  associated.options[:root] = associated.options[:embed_namespace]
  associated.options[:embed] = :objects
  associated.options[:include] = true
  object.whatsits
end

Hoping that would get the Serializer to emit the whatsits list under the _embedded key, but alas that didn't work.. 希望这能使Serializer在_embedded键下发出whatsits列表,但可惜没有用。

This works. 这可行。

class MyThingySerializer < ActiveModel::Serializer
  root false

  attributes :id, :name, :description, :_embedded

  # force the whatsits list to sit under '_embedded'.
  def _embedded
    {
      whatsits: object. whatsits.map {|whatsit| WhatsitSerializer.new(whatsit) } || []
    }
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将has_many添加到ActiveModel :: Serializer时出现问题 - Issue adding has_many to ActiveModel::Serializer ActiveModel :: Serializer has_many对象的哈希值 - ActiveModel::Serializer has_many hash of objects 如何通过与ActiveModel :: Serializer关联显示has_many: - How can show has_many :through Association with ActiveModel::Serializer ActiveModel :: Serializer:与联接表一起使用的推荐方式(has_many至) - ActiveModel::Serializer: Recommended way to use with join table (has_many through) 具有has_and_belongs_to_many的ActiveModel Serializer - ActiveModel Serializer with has_and_belongs_to_many 如何将 ActiveModel::Serializer:has_many 关联的密钥格式(即驼峰式)指定为一次性选项(不是全局配置)? - How to specify the key format (i.e. camelcase) for an ActiveModel::Serializer :has_many association as a one-off option (not global config)? Rails 4:在序列化器中订购has_many - Rails 4: Order has_many in serializer 用于 has_many 查询的 Jsonapi 序列化程序参数 - Jsonapi serializer parameters to has_many query Rails ActiveModel 使用单个 class 设计 belongs_to 和 has_many - Rails ActiveModel designing belongs_to and has_many with single class ActiveModel Serializers:has_many 在运行时有条件? - ActiveModel Serializers: has_many with condition at run-time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM