serialization - Rails ActiveModel Serializers render not null attributes -
i want use serializer renders not null attributes
class personserializer < activemodel::serializer attributes :id, :name, :phone, :address, :email end
is possible.
many thanks.
solution:
class personserializer < activemodel::serializer attributes :id, :name, :phone, :address, :email def attributes hash = super hash.each {|key, value| if value.nil? hash.delete(key) end } hash end end
from version 0.10.x of active_model_serializer
gem, have override method serializable_hash
instead of attributes
:
# place method inside nullattributesremover or directly inside serializer class def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance) hash = super hash.each { |key, value| hash.delete(key) if value.nil? } hash end
Comments
Post a Comment