diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 9f7c7878..26749f1a 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -26,7 +26,8 @@ module ActiveModel def as_json(*args) @hash = {} - array = serializable_array.as_json(*args) + + array = serializable_array.map(&:serializable_hash) if root = @options[:root] @hash.merge!(root => array) diff --git a/test/serializer_test.rb b/test/serializer_test.rb index 84c1d4f6..838e6a1a 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -416,6 +416,20 @@ class SerializerTest < ActiveModel::TestCase ], serializer.as_json) end + def test_array_serializer + comment1 = Comment.new(:title => "Comment1", :id => 1) + comment2 = Comment.new(:title => "Comment2", :id => 2) + + array = [ comment1, comment2 ] + + serializer = array.active_model_serializer.new(array, nil, :root => :comments) + + assert_equal({ :comments => [ + { :title => "Comment1" }, + { :title => "Comment2" } + ]}, serializer.as_json) + end + class CustomBlog < Blog attr_accessor :public_posts, :public_user end