diff --git a/lib/active_model/serializer/array_serializer.rb b/lib/active_model/serializer/array_serializer.rb index fa66e290a..a7e27a560 100644 --- a/lib/active_model/serializer/array_serializer.rb +++ b/lib/active_model/serializer/array_serializer.rb @@ -29,7 +29,7 @@ def json_key if @objects.first @objects.first.json_key.pluralize else - @resource.name.downcase.pluralize if @resource.try(:name) + @resource.name.underscore.pluralize if @resource.try(:name) end end diff --git a/test/array_serializer_test.rb b/test/array_serializer_test.rb index 259793f79..b7ee709c4 100644 --- a/test/array_serializer_test.rb +++ b/test/array_serializer_test.rb @@ -38,6 +38,17 @@ def test_meta_and_meta_key_attr_readers assert_equal @serializer.meta, "the meta" assert_equal @serializer.meta_key, "the meta key" end + + def test_json_key_when_resource_is_empty + Array.class_eval do + def name + 'PostComment' + end + end + @post_comments = [] + @serializer = ArraySerializer.new(@post_comments) + assert_equal @serializer.json_key, "post_comments" + end end end end