【问题标题】:Active Model Serializers belongs_to活动模型序列化器 belongs_to
【发布时间】:2012-10-29 16:00:18
【问题描述】:

这个问题与 AMS 0.8 相关

我有两个模型:

class Subject < ActiveRecord::Base
  has_many :user_combinations
  has_ancestry
end

class UserCombination < ActiveRecord::Base
  belongs_to :stage
  belongs_to :subject
  belongs_to :user
end

还有两个序列化器:

class UserCombinationSerializer < ActiveModel::Serializer
      attributes :id
      belongs_to :stage
      belongs_to :subject
end

class SubjectSerializer < ActiveModel::Serializer
  attributes :id, :name, :description, :subjects

  def include_subjects?
    object.is_root?
  end

  def subjects
    object.subtree
  end
end

UserCombination被序列化时,我想嵌入整个主题子树。

当我尝试使用此设置时,我收到此错误:

undefined method `belongs_to' for UserCombinationSerializer:Class

我尝试将UserCombinationSerializer 更改为:

class UserCombinationSerializer < ActiveModel::Serializer
  attributes :id, :subject, :stage
end

在这种情况下,我没有收到任何错误,但 subject 以错误的方式序列化 - 未使用 SubjectSerializer

我的问题:

  1. 难道我不能在序列化程序中使用belongs_to 关系吗?
  2. 如果不是 - 我怎样才能获得想要的行为 - 使用 SubjectSerializer 嵌入主题树?

【问题讨论】:

    标签: ruby-on-rails active-model-serializers


    【解决方案1】:

    这不是很优雅,但它似乎正在工作:

    class UserCombinationSerializer < ActiveModel::Serializer
      attributes :id, :stage_id, :subject_id
    
      has_one :subject
    end
    

    我不太喜欢调用 has_one 而它实际上是一个 belongs_to 关联:/

    编辑:忽略我关于 has_one/belongs_to 歧义的评论,文档实际上对此非常清楚:http://www.rubydoc.info/github/rails-api/active_model_serializers/frames

    【讨论】:

    • 好的,是的,这行得通。我想我现在更好地理解了has_one 方法。在Serializer 中,唯一有趣的是方法是否返回一个或多个对象。所以区分 has_one 和 belongs_to 并不有趣。措辞与 ActiveRecord 术语一致是一种次优,因为这些术语的含义不同。
    • 我最近遇到了同样的问题。是的,使用 has_one :attribute 对我有用。
    • ActiveModel::Serializer 的文档明确指出:“序列化程序只关心多重性,而不关心所有权。belongs_to ActiveRecord 关联可以使用 has_one 包含在您的序列化程序中。”
    • @awendt 请问这是在哪里明确说明的?我似乎找不到那个。我并不是说你错了,我对 rails/ruby 世界还是新手,并且在查找文档方面并没有像使用 Java 那样出色和快速。这让我明白了真正的意义,如果它不明显,它就不是明确的。
    • @Andy 根据我的经验,遗憾的是没有中心位置可以找到文档。他们中的很多人都有关于 ruby​​ 文档的文档,很多人在 github 上有他们,可悲的是很多人也没有。大多数时候,我最终会在谷歌上花费大量时间。
    【解决方案2】:

    在 Active Model Serializer 0-10-stable 中,belongs_to 现在可用。

    belongs_to :author, serializer: AuthorPreviewSerializer
    belongs_to :author, key: :writer
    belongs_to :post
    belongs_to :blog
    def blog
      Blog.new(id: 999, name: 'Custom blog')
    end
    

    https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/serializers.md#belongs_to

    所以你可以这样做:

    class UserCombinationSerializer < ActiveModel::Serializer
      attributes :id
      belongs_to :stage, serializer: StageSerializer
      belongs_to :subject, serializer: SubjectSerializer
    end
    

    【讨论】:

      【解决方案3】:

      如果你尝试这样的事情会怎样:

      class UserCombinationSerializer < ActiveModel::Serializer
        attributes :subject,
                   :stage,
                   :id
      
        def subject
          SubjectSerializer.new(object.subject, { root: false } )
        end
      
        def stage
          StageSerializer.new(object.stage, { root: false } )
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 2016-01-23
        相关资源
        最近更新 更多