2015-02-06 71 views
1

我有ProfileSerializer:如何避免在has_one關聯中的串行器中嵌套?

class ProfileSerializer < ActiveModel::Serializer 
    attributes :id, :role, :name 
    has_one :company 
end 

,我得到

{"user": {"id":7,"role":"guest","name":"misa","company":{"id":2,"user_id":7, ...}} 

我有任何機會,以避免 「公司」 築巢和獲取JSON這樣的:

{"user": {"user_info": {"id":7,"role":"guest","name":"misa"}, "company_info": {"id":2,"user_id":7, ...}}} 

回答

0

你可以試試這個:

class ProfileSerializer < ActiveModel::Serializer 
    attributes :id, :role, :name, :company_info 

    def company_info 
    object.company 
    end 
end