diff --git a/lib/active_model/serializer/association/has_many.rb b/lib/active_model/serializer/association/has_many.rb index 52d66d26..f05e9218 100644 --- a/lib/active_model/serializer/association/has_many.rb +++ b/lib/active_model/serializer/association/has_many.rb @@ -4,12 +4,11 @@ module ActiveModel class HasMany < Association def initialize(name, *args) super - @root_key = @embedded_key + @root_key = @embedded_key.to_s @key ||= case CONFIG.default_key_type when :name then name.to_s.pluralize else "#{name.to_s.singularize}_ids" end - end def serializer_class(object, _) @@ -37,4 +36,4 @@ module ActiveModel end end end -end \ No newline at end of file +end diff --git a/test/integration/active_record/active_record_test.rb b/test/integration/active_record/active_record_test.rb index e65f7bb9..14ca1982 100644 --- a/test/integration/active_record/active_record_test.rb +++ b/test/integration/active_record/active_record_test.rb @@ -49,9 +49,9 @@ module ActiveModel 'ar_tag_ids' => [1, 2], 'ar_section_id' => 1 }, - ar_comments: [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] }, + 'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] }, { body: 'i liked it', 'ar_tag_ids' => [3, 1] }], - ar_tags: [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }], + 'ar_tags' => [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }], 'ar_sections' => [{ 'name' => 'ruby' }] }, post_serializer.as_json) end diff --git a/test/unit/active_model/array_serializer/serialization_test.rb b/test/unit/active_model/array_serializer/serialization_test.rb index 01f55b30..4d3a71dc 100644 --- a/test/unit/active_model/array_serializer/serialization_test.rb +++ b/test/unit/active_model/array_serializer/serialization_test.rb @@ -85,7 +85,7 @@ module ActiveModel {title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) }, {title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) } ], - comments: [ + 'comments' => [ {content: "C1"}, {content: "C2"}, {content: "C3"}, diff --git a/test/unit/active_model/serializer/has_many_polymorphic_test.rb b/test/unit/active_model/serializer/has_many_polymorphic_test.rb index e5180cfe..d25a0988 100644 --- a/test/unit/active_model/serializer/has_many_polymorphic_test.rb +++ b/test/unit/active_model/serializer/has_many_polymorphic_test.rb @@ -140,7 +140,7 @@ module ActiveModel { id: c.object_id, type: model_name(c) } end, }, - attachments: [ + 'attachments' => [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] @@ -153,7 +153,7 @@ module ActiveModel assert_equal({ 'mail' => { body: 'Body 1' }, - attachments: [ + 'attachments' => [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] @@ -178,7 +178,7 @@ module ActiveModel { id: c.object_id, type: model_name(c) } end }, - attachments: [ + 'attachments' => [ { type: :image, image: { fake: 'fake' }}, { type: :video, video: { fake: 'fake' }} ] diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index f8b2c738..c6699150 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -111,7 +111,7 @@ module ActiveModel assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, - comments: [{ content: 'C1' }, { content: 'C2' }] + 'comments' => [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.as_json) end @@ -128,7 +128,7 @@ module ActiveModel name: 'Name 1', posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }] }, - comments: [{ content: 'C1' }, { content: 'C2' }] + "comments" => [{ content: 'C1' }, { content: 'C2' }] }, category_serializer.as_json) end @@ -138,7 +138,7 @@ module ActiveModel assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1' }, - comments: [{ content: 'C1' }, { content: 'C2' }] + 'comments' => [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.as_json) end @@ -155,7 +155,7 @@ module ActiveModel assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, - comments: [{ content: 'C1!' }, { content: 'C2!' }] + 'comments' => [{ content: 'C1!' }, { content: 'C2!' }] }, @post_serializer.as_json) end @@ -170,7 +170,7 @@ module ActiveModel assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, - comments: { my_content: ['fake'] } + 'comments' => { my_content: ['fake'] } }, @post_serializer.as_json) end @@ -191,16 +191,16 @@ module ActiveModel @association.embed_in_root_key = :linked @association.embed = :ids assert_equal({ + 'post' => { + title: 'Title 1', body: 'Body 1', + 'comment_ids' => @post.comments.map(&:object_id) + }, linked: { - comments: [ + 'comments' => [ { content: 'C1' }, { content: 'C2' } ] }, - 'post' => { - title: 'Title 1', body: 'Body 1', - 'comment_ids' => @post.comments.map(&:object_id) - } }, @post_serializer.as_json) end @@ -211,18 +211,18 @@ module ActiveModel @association.embed_namespace = :links @association.key = :comments assert_equal({ - linked: { - comments: [ - { content: 'C1' }, - { content: 'C2' } - ] - }, 'post' => { title: 'Title 1', body: 'Body 1', links: { comments: @post.comments.map(&:object_id) } - } + }, + linked: { + 'comments' => [ + { content: 'C1' }, + { content: 'C2' } + ] + }, }, @post_serializer.as_json) end