ruby on rails - Embedding IDs with active model serializers leaves out key names when using composite primary keys -
i using composite primary keys in ruby on rails api server. using active model serializers manage serialization of models. use embed: :ids
feature in serializer embed ids. works composite primary keys, gives id's values. since there multiple ids single record, there no way know value belongs column.
my serializer
class postserializer < activemodel::serializer attributes :id, :title, :body has_many :comments, embed :ids end
resulting json
{ "post": { "id": 1, "title": "new post", "body": "a body!", "comment_ids": [ [1, "v1"], [1, "v2"], [2, "v1"] ] } }
i able work around issue using active model serilizer's embed_key
option along composite primary key's ids_hash
method.
my serializer
class postserializer < activemodel::serializer attributes :id, :title, :body has_many :comments, embed :ids, embed_keys: :ids_hash end
resulting json
{ "post": { "id": 1, "title": "new post", "body": "a body!", "comment_ids": [ {"comment_id": 1, "version_id": "v1"}, {"comment_id": 1, "version_id": "v2"}, {"comment_id": 2, "version_id": "v1"}, ] } }
Comments
Post a Comment