Skip to main content

json_serialization

⚙️ Optimize JSON Responses with ActiveModel::Serializers

Use ActiveModel::Serializers to control the JSON structure of your API responses. Define serializers for each resource and customize attributes and associations. This approach ensures consistent and efficient payloads.

# app/serializers/post_serializer.rb
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :created_at
belongs_to :user
has_many :comments
end

# In controller
def show
post = Post.find(params[:id])
render json: post, serializer: PostSerializer
end