Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何避免嵌套在一个关联上的序列化程序中?_Ruby On Rails_Api_Active Model Serializers - Fatal编程技术网

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