mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Fix #955
This commit is contained in:
committed by
João Moura
parent
3710c32cee
commit
cf77786da2
@@ -206,10 +206,22 @@ module ActiveModel
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(association_value, association_options)
|
||||
|
||||
if serializer_class
|
||||
serializer = serializer_class.new(
|
||||
association_value,
|
||||
options.except(:serializer).merge(serializer_from_options(association_options))
|
||||
)
|
||||
begin
|
||||
serializer = serializer_class.new(
|
||||
association_value,
|
||||
options.except(:serializer).merge(serializer_from_options(association_options))
|
||||
)
|
||||
rescue NoMethodError
|
||||
# 1. Failure to serialize an element in a collection, e.g. [ {hi: "Steve" } ] will fail
|
||||
# with NoMethodError when the ArraySerializer finds no serializer for the hash { hi: "Steve" },
|
||||
# and tries to call new on that nil.
|
||||
# 2. Convert association_value to hash using implicit as_json
|
||||
# 3. Set as virtual value (serializer is nil)
|
||||
# 4. Consider warning when this happens
|
||||
virtual_value = association_value
|
||||
virtual_value = virtual_value.as_json if virtual_value.respond_to?(:as_json)
|
||||
association_options[:association_options][:virtual_value] = virtual_value
|
||||
end
|
||||
elsif !association_value.nil? && !association_value.instance_of?(Object)
|
||||
association_options[:association_options][:virtual_value] = association_value
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user