Ruby on rails 如何避免嵌套在一个关联上的序列化程序中?
我有一个序列化程序:Ruby on rails 如何避免嵌套在一个关联上的序列化程序中?,ruby-on-rails,api,active-model-serializers,Ruby On Rails,Api,Active Model Serializers,我有一个序列化程序: class ProfileSerializer < ActiveModel::Serializer attributes :id, :role, :name has_one :company end 我认为有任何机会避免“公司”嵌套并获得如下JSON: {"user": {"user_info": {"id":7,"role":"guest","name":"misa"}, "company_info": {"id":2,"user_id":7, ...}}
class ProfileSerializer < ActiveModel::Serializer
attributes :id, :role, :name
has_one :company
end
我认为有任何机会避免“公司”嵌套并获得如下JSON:
{"user": {"user_info": {"id":7,"role":"guest","name":"misa"}, "company_info": {"id":2,"user_id":7, ...}}}
你可以试试这个:
class ProfileSerializer < ActiveModel::Serializer
attributes :id, :role, :name, :company_info
def company_info
object.company
end
end
class ProfileSerializer
class ProfileSerializer < ActiveModel::Serializer
attributes :id, :role, :name, :company_info
def company_info
object.company
end
end