【发布时间】:2015-03-19 02:37:24
【问题描述】:
我有以下ActiveModel::Serializer 类
class MyThingySerializer < ActiveModel::Serializer
root false
attributes :id, :name, :description
has_many :whatsits, embed_namespace: :_embedded
delegate :whatsits, to: :object
end
它在 AMS 0.9.2 下运行良好,但是为了使用 include_attributename? 机制添加可选属性,我被告知回滚到 AMS 0.8
现在我的whatsits 不会出现在_embedded 属性下的序列化输出中。
我需要做一些特别的事情来恢复我嵌入的 whatsits 吗?
更新
我已尝试将以下方法添加到我的序列化程序:
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
希望这将使序列化程序在_embedded 键下发出whatsits 列表,但可惜这不起作用..
【问题讨论】:
-
0.8 的 api 在这里github.com/rails-api/active_model_serializers/tree/… 没有
embed_namespace。可能是has_many :whatsits, root: :_embedded -
唉,没有将
embed_namespace更改为root没有帮助。我想我只需要手动完成。在 AMS v0.10 上滚动
标签: ruby-on-rails active-model-serializers