【问题标题】:ActiveModel Serializer: access attributes as methodsActiveModel 序列化器:将属性作为方法访问
【发布时间】:2018-10-09 18:04:18
【问题描述】:

如何将序列化程序中定义的属性作为方法访问?

class Test < ActiveModel::Serializer 
  attributes :a, :b, :c
end

s = Test.new(Object)
s.a

我需要这个来进行委托,它曾经在 0.8 版/中可用

【问题讨论】:

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


    【解决方案1】:

    序列化器上不再定义属性方法,必须通过对象显式访问

    class MySerializer
      attributes :foo, :bar
    
      def foo
        bar + 1 # bar does not work, needs to be object.bar in 0.10
      end
    end
    

    https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/howto/upgrade_from_0_8_to_0_10.md#010-breaking-changes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      相关资源
      最近更新 更多