Skip to content

Commit 34d684e

Browse files
committed
added failing spec to show how both symbol and string keys are causing problems
1 parent 89f87bf commit 34d684e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test/fixtures/poro.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def comments
4747
end
4848
end
4949

50+
class SpecialPost < Post
51+
def special_comment
52+
@speical_comment ||= Comment.new(content: 'special')
53+
end
54+
end
55+
5056
class Comment < Model
5157
end
5258

@@ -110,6 +116,12 @@ class PostSerializer < ActiveModel::Serializer
110116
has_many :comments
111117
end
112118

119+
class SpecialPostSerializer < ActiveModel::Serializer
120+
attributes :title, :body
121+
has_many :comments, root: :comments, embed_in_root: true, embed: :ids
122+
has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids
123+
end
124+
113125
class CommentSerializer < ActiveModel::Serializer
114126
attributes :content
115127
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class HasOneAndHasManyTest < Minitest::Test
6+
def setup
7+
@post = SpecialPost.new({ title: 'T1', body: 'B1'})
8+
@post_serializer = SpecialPostSerializer.new(@post)
9+
end
10+
11+
def teardown
12+
end
13+
14+
def test_side_load_has_one_and_has_many_in_same_array
15+
assert_equal({
16+
"post" => {
17+
title: 'T1',
18+
body: 'B1',
19+
'comment_ids' => @post.comments.map { |c| c.object_id },
20+
'special_comment_id' => @post_serializer.special_comment.object_id,
21+
},
22+
"comments" => [{ content: 'C1' }, { content: 'C2' }, { content: 'special' }]
23+
}, @post_serializer.as_json(root: 'post'))
24+
end
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)