ruby on rails - How do I select which attributes I want for active model serializers relationships -
i using jsonapi format along active model serializers create api rails-api.
i have serializer shows specific post
has many topics
, currently, under relationships, lists topics. lists id , type. want show title of topic well.
some use include: 'topics'
in controller, don't need full topic record, title.
question: how specify attributes want show topics?
what have
"data": { "id": "26", "type": "posts", "attributes": { "title": "test title 11" }, "relationships": { "topics": { "data": [ { "id": "1", "type": "topics" } ] } } }
what want
"data": { "id": "26", "type": "posts", "attributes": { "title": "test title 11" }, "relationships": { "topics": { "data": [ { "id": "1", "type": "topics", "title": "topic title" } ] } } }
my current serializer classes edit: i'm after please.
class postserializer < activemodel::serializer attributes :title belongs_to :domain belongs_to :user has_many :topics, serializer: topicserializer def topics # asking end end class topicserializer < activemodel::serializer attributes :title, :description belongs_to :parent has_many :children end
one thing tried - there answer below makes work, not i'm after.
class postserializer < activemodel::serializer attributes :title, :topics belongs_to :domain belongs_to :user def topics # answered below! thank end end
just make sure return hash or array of hashes so:
def videos object.listing_videos.collect |lv| { id: lv.video.id, name: lv.video.name, wistia_id: lv.video.wistia_id, duration: lv.video.duration, wistia_hashed_id: lv.video.wistia_hashed_id, description: lv.video.description, thumbnail: lv.video.thumbnail } end end
Comments
Post a Comment