I am trying to create a JSON style API using AM :: Serializer. I encountered a problem while loading.
I want to be able to create JSON that looks like this:
{ "primaries": [{ "id": 123, "data": "Hello world.", "links": { "secondaries": [ 1, 2, 3 ] } }], "linked" : { "secondaries": [ { "id": 1, "data": "test1" }, { "id": 2, "data": "test2" }, { "id": 3, "data": "test3" } ] } }
The code I could find looks like this:
class PrimarySerializer < ActiveModel::Serializer attributes :id, :data has_many :secondaries, key: :secondaries, root: :secondaries embed :ids, include: true end
What generates JSON that looks like this:
{ "primaries": [{ "id": 123, "data": "Hello world.", "secondaries": [ 1, 2, 3 ] }], "secondaries": [ { "id": 1, "data": "test1" }, { "id": 2, "data": "test2" }, { "id": 3, "data": "test3" } ] }
Is there a way to redefine the location of in-element secondaries and sideloaded secondaries so that they are in the link and linked child nodes?
The above code is an abstraction of the actual code and may not work. I hope he illustrates this point quite well.
Thanks!