2015-03-19 21 views
0

我有以下ActiveModel::Serializer加載ActiveModel遷移::串行從v0.9.2到0.8了,沒有的has_many被​​序列化

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名單,但很可惜的是沒't work ..

+0

0.8的API是這裏https://github.com/rails-api/active_model_serializers/tree/8d31f7ee8c0ea3ed83ab871765f1444291278b41#embedding-associations沒有'embed_namespace'。可能'has_many:whatsits,root::_embedded' – robertjlooby 2015-03-19 13:10:02

+0

唉,沒有把'embed_namespace'改爲'root'沒有幫助。我只需要手動去做就可以了。卷在AMS v0.10上 – 2015-03-19 21:57:07

回答

0

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