Ruby on rails ActiveModel序列化程序继承
假设我有这个序列化程序Ruby on rails ActiveModel序列化程序继承,ruby-on-rails,active-model-serializers,Ruby On Rails,Active Model Serializers,假设我有这个序列化程序 class FooSerializer < ActiveModel::Serializer attributes :this, :that, :the_other def this SomeThing.expensive(this) end def that SomeThing.expensive(that) end def the_other
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 FooSerializer
其中单个序列化值的操作有些昂贵
然后我有另一个序列化程序,可以利用它,但不返回所有成员:
class BarSerializer < FooSerializr
attributes :the_other
end
class BarSerializer
这不管用。。。BarSerializer仍然会有这个、那个和其他
如何利用继承而不自动获得相同的属性?我正在寻找模块混合以外的解决方案。结果证明,解决这个问题的办法是利用“包含”的魔力?方法
class BarSerializer < FooSerializr
def include_this?; false; end
def include_that?; false; end
end
class BarSerializer
这将使它只序列化“theu other”使
BarSerializer
成为父类,并将方法theu other
放入其中FooSerializer
将只继承BarSerializer
中定义的方法和属性