pat*_*ick 13 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
SomeThing.expensive(the_other)
end
end
Run Code Online (Sandbox Code Playgroud)
单个序列化值的操作有些昂贵...
然后我有另一个序列化器,它可以使用它,但不会返回所有成员:
class BarSerializer < FooSerializr
attributes :the_other
end
Run Code Online (Sandbox Code Playgroud)
这不起作用...... BarSerializer仍然会有这个,那个和另外的......
如何使用继承但不能自动获取相同的属性?我正在寻找除模块mixins之外的解决方案.
原来这个答案就是利用魔法include_xxx?方法...
class BarSerializer < FooSerializr
def include_this?; false; end
def include_that?; false; end
end
Run Code Online (Sandbox Code Playgroud)
这将使它只序列化"the_other"