Rails ActiveModel Serializers渲染非null属性

时间:2023-02-08 11:44:05

I want to use a serializer that renders not null attributes

我想使用一个渲染非null属性的序列化程序

     class PersonSerializer < ActiveModel::Serializer
        attributes :id, :name, :phone, :address, :email
     end

Is this 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

4 个解决方案

#1


13  

From version 0.10.x of active_model_serializer gem, you have to override the method serializable_hash instead of attributes:

从active_model_serializer gem的0.10.x版本开始,您必须覆盖方法serializable_hash而不是属性:

# place this 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

#2


9  

Thanks Nabila Hamdaoui for your solution. I made it a little more reusable via modules.

感谢Nabila Hamdaoui的解决方案。我通过模块使其更加可重复使用。

null_attribute_remover.rb

module NullAttributesRemover
  def attributes
    hash = super
    hash.each do |key, value|
      if value.nil?
        hash.delete(key)
      end
    end
    hash
  end
end

Usage:

swimlane_serializer.rb

class SwimlaneSerializer < ActiveModel::Serializer
  include NullAttributesRemover
  attributes :id, :name, :wipMaxLimit
end

#3


0  

class ActiveModel::Serializer
  def attributes
    filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
      val = send(name)
      hash[name] = val unless val.nil?
    end
  end
end

#4


-2  

Please add validation presence:true in your Person model for (:id, :name, :phone, :address, :email) attributes, so you will get not null JSON value while you render.

请在您的Person模型中为(:id,:name,:phone,:address,:email)属性添加验证状态:true,这样在渲染时您将获得非空JSON值。

#1


13  

From version 0.10.x of active_model_serializer gem, you have to override the method serializable_hash instead of attributes:

从active_model_serializer gem的0.10.x版本开始,您必须覆盖方法serializable_hash而不是属性:

# place this 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

#2


9  

Thanks Nabila Hamdaoui for your solution. I made it a little more reusable via modules.

感谢Nabila Hamdaoui的解决方案。我通过模块使其更加可重复使用。

null_attribute_remover.rb

module NullAttributesRemover
  def attributes
    hash = super
    hash.each do |key, value|
      if value.nil?
        hash.delete(key)
      end
    end
    hash
  end
end

Usage:

swimlane_serializer.rb

class SwimlaneSerializer < ActiveModel::Serializer
  include NullAttributesRemover
  attributes :id, :name, :wipMaxLimit
end

#3


0  

class ActiveModel::Serializer
  def attributes
    filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
      val = send(name)
      hash[name] = val unless val.nil?
    end
  end
end

#4


-2  

Please add validation presence:true in your Person model for (:id, :name, :phone, :address, :email) attributes, so you will get not null JSON value while you render.

请在您的Person模型中为(:id,:name,:phone,:address,:email)属性添加验证状态:true,这样在渲染时您将获得非空JSON值。