如何像使用活动模型序列化程序一样,在Rails中使用Rails Netflix fast_jsonapi在序列化程序中定义函数?
答案 0 :(得分:0)
这很困难。这是issue前一阵子提出来的,但是什么都没有。
发生的事情是串行器有点像单例,所以您基本上被迫使用类方法,例如
class MovieSerializer
include FastJsonapi::ObjectSerializer
attributes :name, :year
attribute :name_with_year do |object|
foo(object)
end
def self.foo(object) # will be called
# do something with object
end
def foo(object) # will never be called
# do something with object
end
end
该宝石的作用域范围之内有一些奇怪的东西,使之成为必需。