ruby on rails - Active Model Serializer not working with json_api adapter -


i trying use custom serializers relationships in serializer , json_api adapter enabled. relationships not serialized correctly (or, better, not @ displayed/serialized).

postcontroller.rb

def index   render json: post.all, each_serializer: serializers::postserializer end 

serializer

module api   module v1     module serializers       class postserializer < activemodel::serializer         attributes :title, :id          belongs_to :author, serializer: userserializer         has_many :post_sections, serializer: postsectionserializer       end     end   end end 

json output:

{     "data": [         {             "attributes": {                 "title": "test title"             },             "id": "1",             "relationships": {                 "author": {                     "data": {                         "id": "1",                         "type": "users"                     }                 },                 "post_sections": {                     "data": [                         {                             "id": "1",                             "type": "post_sections"                         }                     ]                 }             },             "type": "posts"         }     ] } 

as can see, relationships not fulfilled, happens only if specify custom serializer relationships!!

if this:

module api   module v1     module serializers       class postserializer < activemodel::serializer         attributes :title, :id          belongs_to :author # no custom serializer!         has_many :post_sections # no custom serializer!       end     end   end end 

the relationships shown correctly, not using custom serializer... what's issue here ?

edit

according json api 1.0 format, getting so-called resource identifier object.

the following primary data single resource identifier object references same resource:

{ "data": { "type": "articles", "id": "1" } }

is there way prevent getting resource identifier objects, , actual data instead ?

relationships returns id , type according json-api exmaples. if need return more information relation should add include option on render action.

ex.

postcontroller.rb

class postscontroller < applicationcontroller   def show     render json: @post, include: 'comments'   end end 

serializers

class postserializer < activemodel::serializer   attributes :id, :title, :content   has_many :comment, serializer: commentserializer end  class commentserializer < activemodel::serializer   attributes :id, :title, :content end 

json output:

{     "data": {         "id": "1",         "type": "post",         "attributes": {             "title": "bla",             "content": "bla"         },         "relationships": {             "comment": {                 "data": [                     {                         "type": "comments",                         "id": "1"                     }                 ]             }         }     },     "included": {         {             "id": "1",             "type": "comments",             "attributes": {                 "title": "test",                 "content": "test"             }         }     ] } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -