[英]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.