ruby 39 lines · 7 steps

Building a JSON:API endpoint in Rails

A versioned API controller that paginates, eager-loads, and serializes articles while avoiding N+1 queries.

Explained by highlit
1class Api::V1::ArticlesController < Api::V1::BaseController
2 def index
3 articles = Article
4 .published
5 .includes(:author, :categories, comments: :author, cover_image_attachment: :blob)
6 .order(published_at: :desc)
7 .page(params[:page])
8 .per(25)
9 
10 render json: ArticleSerializer.new(
11 articles,
12 include: %i[author categories comments comments.author],
13 meta: pagination_meta(articles)
14 ).serializable_hash
15 end
16 
17 def show
18 article = Article
19 .includes(:author, :categories, { comments: %i[author reactions] })
20 .find(params[:id])
21 
22 render json: ArticleSerializer.new(
23 article,
24 include: %i[author categories comments comments.author comments.reactions]
25 ).serializable_hash
26 end
27 
28 private
29 
30 def pagination_meta(scope)
31 {
32 current_page: scope.current_page,
33 next_page: scope.next_page,
34 prev_page: scope.prev_page,
35 total_pages: scope.total_pages,
36 total_count: scope.total_count
37 }
38 end
39end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Chaining includes with nested hashes preloads deep associations in one pass, sidestepping N+1 queries.
  2. 2Pagination metadata belongs in a reusable helper so multiple actions return a consistent response shape.
  3. 3A serializer's include option controls exactly which related records appear, keeping payloads intentional.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Building a JSON:API endpoint in Rails — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code