Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 Rails活动\u模型\u序列化程序关系未序列化_Ruby On Rails_Ruby_Serialization_Ruby On Rails 5_Active Model Serializers - Fatal编程技术网

Ruby on rails Rails活动\u模型\u序列化程序关系未序列化

Ruby on rails Rails活动\u模型\u序列化程序关系未序列化,ruby-on-rails,ruby,serialization,ruby-on-rails-5,active-model-serializers,Ruby On Rails,Ruby,Serialization,Ruby On Rails 5,Active Model Serializers,我正在用active_model_序列化程序gem构建一个Rails API 我遇到的问题是,我无法序列化关系,即使我为它们创建了序列化程序 首先,以下是我的模型: user.rb class User < ApplicationRecord has_many :component class用户

我正在用active_model_序列化程序gem构建一个Rails API

我遇到的问题是,我无法序列化关系,即使我为它们创建了序列化程序

首先,以下是我的模型:

user.rb

class User < ApplicationRecord
has_many :component
class用户
component.rb

class Component < ActiveRecord::Base
  belongs_to :user

  has_many :profiles
end
class组件
这是我的用户_controller.rb:

class UsersController < ApplicationController

    def show
        @user = User.first

        render(                              //
            status: :ok,                     //
            json: @user,                     //updated
            serializer: UserSerializer       //
        )                                    //
    end
end
class UsersController
还有我的序列化程序

user_serializer.rb

class UserSerializer < ApplicationSerializer
  attributes :id, :component

  has_many :component, serializer: ComponentSerializer //updated
end
class ComponentSerializer < UserSerializer
  attribute :id, :profile, :test

  def test
    'this is a test'
  end
end
class UserSerializer
组件_serializer.rb

class UserSerializer < ApplicationSerializer
  attributes :id, :component

  has_many :component, serializer: ComponentSerializer //updated
end
class ComponentSerializer < UserSerializer
  attribute :id, :profile, :test

  def test
    'this is a test'
  end
end
class ComponentSerializer
正如您在所附图像中看到的,我可以序列化“用户”的属性,但无法编辑“组件”的属性

如果我删除文件'component_serializer.rb',所有属性都会列在关系属性的'component'下,如第二个屏幕截图所示

我遗漏了什么吗?我现在搜索了很长时间,但没有找到答案

我不知道这是否重要,但我正在linux服务器上使用Rails 5.0.1,带有“活动的”\u model\u序列化程序”、“~>0.10.0”gem


提前谢谢

好的,搜索了一会儿,我才发现,这个问题已经被问过了^^

有人能告诉我这是否是序列化嵌套模型的推荐方法吗


只是一个快速更新。如果其他人正在处理此问题

GoRails的本教程解释了为什么序列化程序不影响模型关系中的属性,以及如何包含嵌套模型