簡體   English   中英

ActiveModel序列化器:將屬性作為方法訪問

[英]ActiveModel Serializer: access attributes as methods

如何將序列化程序中定義的屬性作為方法訪問?

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

s = Test.new(Object)
s.a

我需要進行委派,它以前在版本0.8 /中可用

屬性方法不再在序列化程序上定義,而必須通過對象顯式訪問

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM