File tree Expand file tree Collapse file tree 2 files changed +49
-2
lines changed Expand file tree Collapse file tree 2 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -165,6 +165,53 @@ def test_json_api_serialization
165165
166166 assert_equal ( expected , serialization ( @picture , :json_api ) )
167167 end
168+
169+ def test_json_api_serialization_with_polymorphic_belongs_to
170+ expected = {
171+ data : {
172+ id : '1' ,
173+ type : 'poly-tags' ,
174+ attributes : { phrase : 'foo' } ,
175+ relationships : {
176+ :"object-tags" => {
177+ data : [
178+ { id : '1' , type : 'object-tags' } ,
179+ { id : '5' , type : 'object-tags' }
180+ ]
181+ }
182+ }
183+ } ,
184+ included : [
185+ {
186+ id : '1' ,
187+ type : 'object-tags' ,
188+ relationships : {
189+ taggable : {
190+ data : { id : '42' , type : 'employees' }
191+ }
192+ }
193+ } ,
194+ {
195+ id : '42' ,
196+ type : 'employees'
197+ } ,
198+ {
199+ id : '5' ,
200+ type : 'object-tags' ,
201+ relationships : {
202+ taggable : {
203+ data : { id : '1' , type : 'pictures' }
204+ }
205+ }
206+ } ,
207+ {
208+ id : '1' ,
209+ type : 'pictures'
210+ }
211+ ]
212+ }
213+ assert_equal ( expected , tag_serialization ( :json_api ) )
214+ end
168215 end
169216 end
170217 end
Original file line number Diff line number Diff line change @@ -89,7 +89,7 @@ class ObjectTag < ActiveRecord::Base
8989end
9090class PolymorphicObjectTagSerializer < ActiveModel ::Serializer
9191 attributes :id
92- has_many :taggable , serializer : PolymorphicSimpleSerializer , polymorphic : true
92+ belongs_to :taggable , serializer : PolymorphicSimpleSerializer , polymorphic : true
9393end
9494
9595class PolyTag < ActiveRecord ::Base
@@ -109,5 +109,5 @@ class PolymorphicHasManySerializer < ActiveModel::Serializer
109109end
110110class PolymorphicBelongsToSerializer < ActiveModel ::Serializer
111111 attributes :id , :title
112- has_one :imageable , serializer : PolymorphicHasManySerializer , polymorphic : true
112+ belongs_to :imageable , serializer : PolymorphicHasManySerializer , polymorphic : true
113113end
You can’t perform that action at this time.
0 commit comments