mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Handle correctly null associations
null belongs_to associations are now serialized as nil instead of raise an error during serialization.
This commit is contained in:
@@ -13,7 +13,11 @@ module ActiveModel
|
||||
array_serializer = association
|
||||
@result[name] = array_serializer.map { |item| item.attributes(opts) }
|
||||
else
|
||||
@result[name] = association.attributes(options)
|
||||
if association
|
||||
@result[name] = association.attributes(options)
|
||||
else
|
||||
@result[name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,13 +44,17 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def add_link(name, serializer, options)
|
||||
@hash[@root][:links][name] = serializer.id.to_s
|
||||
if serializer
|
||||
@hash[@root][:links][name] = serializer.id.to_s
|
||||
|
||||
unless options[:embed] == :ids
|
||||
plural_name = name.to_s.pluralize.to_sym
|
||||
unless options[:embed] == :ids
|
||||
plural_name = name.to_s.pluralize.to_sym
|
||||
|
||||
@hash[:linked][plural_name] ||= []
|
||||
@hash[:linked][plural_name].push attributes_for_serializer(serializer, options)
|
||||
@hash[:linked][plural_name] ||= []
|
||||
@hash[:linked][plural_name].push attributes_for_serializer(serializer, options)
|
||||
end
|
||||
else
|
||||
@hash[@root][:links][name] = nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user