active_model_serializer_example
🔧 Leverage ActiveModel::Serializers for Structured APIs​
ActiveModel::Serializer allows you to extract JSON structure into dedicated classes, giving you reusable, testable serializers. Define attributes, associations, and custom methods in your serializer for consistent API responses.
# app/serializers/user_serializer.rb
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
has_many :posts
def email
object.email.downcase
end
end
# In controller
def show
user = User.find(params[:id])
render json: user, serializer: UserSerializer
end