From 54e028dc6e61037ba1fa60d501749532f84c7db0 Mon Sep 17 00:00:00 2001 From: Sebastian Edwards Date: Wed, 22 Mar 2017 16:24:21 +1300 Subject: [PATCH] Fix error where polymorphic relation not set Currently if you declare a polymorphic association on a resource `AuthorizingProcessor.related_models` will error out if you try to create a record without having that relation set. The `assoc_value` will be `nil` which is not a `Hash` so the polymorphic relationship code route is not be taken. This fixes that. --- lib/jsonapi/authorization/authorizing_processor.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/jsonapi/authorization/authorizing_processor.rb b/lib/jsonapi/authorization/authorizing_processor.rb index 7bedefb2..591d5590 100644 --- a/lib/jsonapi/authorization/authorizing_processor.rb +++ b/lib/jsonapi/authorization/authorizing_processor.rb @@ -249,6 +249,8 @@ def related_models [:to_one, :to_many].flat_map do |rel_type| data[rel_type].flat_map do |assoc_name, assoc_value| case assoc_value + when nil + next when Hash # polymorphic relationship resource_class = @resource_klass.resource_for(assoc_value[:type].to_s) resource_class.find_by_key(assoc_value[:id], context: context)._model @@ -257,7 +259,7 @@ def related_models primary_key = resource_class._primary_key resource_class._model_class.where(primary_key => assoc_value) end - end + end.compact end end