说我有这个序列化程序
class FooSerializer < ActiveModel::Serializer
attributes :this, :that, :the_other
def this
SomeThing.expensive(this)
end
def that
SomeThing.expensive(that)
end
def the_other
SomeThing.expensive(the_other)
end
end
单个序列化值的操作有些昂贵......
然后我有另一个序列化程序可以利用它,但不返回所有成员:
class BarSerializer < FooSerializr
attributes :the_other
end
这不起作用... BarSerializer 仍然会有这个、那个和 the_other...
如何利用继承但不能自动获得相同的属性?我正在寻找模块混合以外的解决方案。
最佳答案
原来这个问题的答案是利用魔法 include_xxx?方法...
class BarSerializer < FooSerializer
def include_this?; false; end
def include_that?; false; end
end
这将使它只序列化“the_other”
关于ruby-on-rails - ActiveModel 序列化程序继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709952/