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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Chaining includes with nested hashes preloads deep associations in one pass, sidestepping N+1 queries.
- 2Pagination metadata belongs in a reusable helper so multiple actions return a consistent response shape.
- 3A serializer's include option controls exactly which related records appear, keeping payloads intentional.
Related explainers
php
<?php namespace App\Http\Controllers;
Building a filtered product index in Laravel
query-builder
validation
conditional-queries
Intermediate
9 steps
ruby
class MetricSeries def initialize(readings, window: 5) @readings = readings @window = window
Rolling averages with each_cons in Ruby
sliding-window
enumerable
data-smoothing
Intermediate
7 steps
rust
use axum::{ extract::{Query, State}, http::StatusCode, Json,
Paginated, filtered product listing in Axum
pagination
query-parameters
sql-filtering
Intermediate
8 steps
ruby
class FundsTransfer class InsufficientFundsError < StandardError; end def initialize(source:, destination:, amount:)
Atomic money transfers with Rails transactions
service object
database transactions
row locking
Advanced
9 steps
ruby
class Money include Comparable CURRENCY_SYMBOLS = { usd: "$", eur: "€", gbp: "£" }.freeze
Building an immutable Money value object in Ruby
value-object
immutability
comparable
Intermediate
8 steps
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-json-api-endpoint-in-rails-explained-ruby-1cca/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.