问题
I am using the JSONAPI format along with Active Model Serializers to create an api with rails-api.
I have a serializer which shows a specific post
that has many topics
and currently, under relationships, lists those topics. It currently only lists the id and type. I want to show the title of the topic as well.
Some would say to use include: 'topics'
in my controller, but I don't need the full topic record, just its title.
Question: How do I specify which attributes I want to show from topics?
What I have
"data": {
"id": "26",
"type": "posts",
"attributes": {
"title": "Test Title 11"
},
"relationships": {
"topics": {
"data": [
{
"id": "1",
"type": "topics"
}
]
}
}
}
What I 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: this is what I'm after please.
class PostSerializer < ActiveModel::Serializer
attributes :title
belongs_to :domain
belongs_to :user
has_many :topics, serializer: TopicSerializer
def topics
# THIS IS WHAT I AM REALLY ASKING FOR
end
end
class TopicSerializer < ActiveModel::Serializer
attributes :title, :description
belongs_to :parent
has_many :children
end
One thing I tried - there is an answer below that makes this work, but its not really what I'm after.
class PostSerializer < ActiveModel::Serializer
attributes :title, :topics
belongs_to :domain
belongs_to :user
def topics
# THIS WAS ANSWERED BELOW! THANK YOU
end
end
回答1:
Just make sure to return a hash or array of hashes like so:
def videos
object.listing_videos.collect do |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
回答2:
Instead of defining topics method, it's better to define separate topic serializer with explicitly specifying which attributes do you need to include. This is cleaner, and more maintainable approach then defining topics method.
class PostSerializer < ActiveModel::Serializer
attributes :title
belongs_to :domain
belongs_to :user
# remember to declare TopicSerializer class before you use it
class TopicSerializer < ActiveModel::Serializer
# explicitly tell here which attributes you need from 'topics'
attributes :title
end
has_many :topics, serializer: TopicSerializer
end
Again, try to avoid defining methods for relations as much as possible, it's not clean, neither maintainable.
来源:https://stackoverflow.com/questions/32854013/how-do-i-select-which-attributes-i-want-for-active-model-serializers-relationshi