Serializerで特定のfieldだけを出力したい
解決
fields
オプションを使用する。
render json: notifications, fields: [:id, :title]
How to filter attributes? · Issue #992 · rails-api/active_model_serializers · GitHub
問題
ActiveModel::MissingAttributeError (missing attribute: description)
が発生する。
構成
class NotificationSerializer < ApplicationSerializer
attribute :id
attribute :title
attribute :description
end
notifications = Notification.select(:id, :title)
render json: notifications