2015-02-06 37 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