Complete the code to define a serializer class for a User model.
class UserSerializer < ActiveModel::Serializer attributes [1] end
The attributes method takes a list of symbols representing the model's fields to include in the serialized output.
Complete the code to add a custom method attribute to the serializer.
class UserSerializer < ActiveModel::Serializer attributes :id, :name, :email, [1] def [1] object.name.upcase end end
The custom attribute name must match the method name defined in the serializer.
Fix the error in the serializer to correctly include an association.
class UserSerializer < ActiveModel::Serializer attributes :id, :name has_many [1] end
Associations must be specified as symbols with the correct pluralization for collections.
Fill both blanks to define a belongs_to association with a custom serializer.
class CommentSerializer < ActiveModel::Serializer attributes :id, :content belongs_to [1], serializer: [2] end
The belongs_to association uses the symbol for the related model and the serializer class name for custom serialization.
Fill in the blanks to create a serializer with conditional attributes and a custom method.
class ProductSerializer < ActiveModel::Serializer attributes :id, :name, [1] attribute :discounted_price, if: -> { [2] } def discounted_price object.price * 0.9 end end
The attributes list includes :price. The conditional attribute uses a lambda checking object.on_sale?.