ruby 38 lines · 9 steps

Shaping API output with ActiveModel::Serializer in Rails

An ArticleSerializer defines exactly which fields, relations, and computed values an article exposes as JSON.

Explained by highlit
1class ArticleSerializer < ActiveModel::Serializer
2 attributes :id, :title, :slug, :excerpt, :reading_time, :published_at, :url
3 
4 belongs_to :author, serializer: UserSerializer
5 has_many :comments, if: :include_comments?
6 has_many :tags
7 
8 meta do
9 { comment_count: object.comments.size }
10 end
11 
12 def excerpt
13 object.body.truncate(200, separator: ' ')
14 end
15 
16 def reading_time
17 minutes = (object.body.split.size / 200.0).ceil
18 "#{minutes} min read"
19 end
20 
21 def published_at
22 object.published_at&.iso8601
23 end
24 
25 def url
26 Rails.application.routes.url_helpers.article_url(object)
27 end
28 
29 def include_comments?
30 instance_options[:with_comments] || current_user&.admin?
31 end
32 
33 private
34 
35 def current_user
36 scope
37 end
38end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A serializer decouples your JSON shape from your database schema, so the API contract stays stable as models change.
  2. 2Overriding an attribute method lets you transform or compute values on the fly instead of exposing raw columns.
  3. 3Conditional associations and scope give you per-request, permission-aware output without branching in the controller.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Shaping API output with ActiveModel::Serializer in Rails — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code