【发布时间】:2014-11-27 12:01:46
【问题描述】:
有没有办法可以将选项传递给新版本的 AMS,例如 this answer 节目?
【问题讨论】:
标签: ruby-on-rails active-model-serializers
有没有办法可以将选项传递给新版本的 AMS,例如 this answer 节目?
【问题讨论】:
标签: ruby-on-rails active-model-serializers
您可以在创建序列化程序的新实例时解析选项Hash,但它将使用的唯一属性是:root,正如您在ActiveModel::Serializer source code 上看到的那样:
def initialize(object, options = {})
@object = object
@root = options[:root] || (self.class._root ? self.class.root_name : false)
end
您可以在您的 Serializer 类上覆盖此方法,并根据需要使用其余选项:
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
def initialize(object, options = {})
super(object, options)
# Your custom code goes here
end
end
【讨论】: