代码之家  ›  专栏  ›  技术社区  ›  RandallB

活动模型序列化程序:如何将选项传递给集合?

  •  0
  • RandallB  · 技术社区  · 7 年前

    如果我有一些东西的集合,比如小部件,并且我正在使用活动模型序列化器来序列化小部件的集合,那么我如何将instance\u选项传递给集合?

    render json: @widgets, count: 40
    

    我试了上述方法,但似乎无法 count: 40 instance_options .我错过什么了吗?

    1 回复  |  直到 7 年前
        1
  •  5
  •   Yongcheol Charles Choi    7 年前

    你可以打电话 @instance_options[:count]

    render json: @widgets, count: 40
    

    对于exmaple,

    class WidgetsSerializer < ActiveModel::Serializer
      attributes :count
    
      def count
        @instance_options[:count] #=> 40
      end
    end