Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ActiveModelSerializersについて
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
yuki21
July 17, 2020
Programming
45
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ActiveModelSerializersについて
5分間社内LT資料
yuki21
July 17, 2020
More Decks by yuki21
See All by yuki21
労務ドメインを快適に開発する方法 / How to Comfortably Develop in the Labor Domain
yuki21
1
480
GitHubのコマンドパレット使ってますか?
yuki21
0
1.7k
キャッシュを利用してRailsアプリの処理を高速化する
yuki21
0
130
Next.js & ElectronでTodoアプリを作る
yuki21
0
740
gRPCを完璧に理解する
yuki21
0
55
RSpec -基本の基-
yuki21
0
59
Committeeを導入してみた
yuki21
0
160
マイクロサービスとモノリスとKBR
yuki21
0
55
脆弱性について
yuki21
0
170
Other Decks in Programming
See All in Programming
Contextとはなにか
chiroruxx
1
390
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
460
信頼性について考えてみる(SRE NEXT 2026 miniLT)
hayama17
0
160
AIエージェントで 変わるAndroid開発環境
takahirom
2
510
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
170
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
150
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
230
Claude Team Plan導入・ガイド
tk3fftk
0
170
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
280
symfony/aiとlaravel/boost
77web
0
120
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
150
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
30 Presentation Tips
portentint
PRO
1
350
Optimising Largest Contentful Paint
csswizardry
37
3.8k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
410
The Invisible Side of Design
smashingmag
301
52k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
590
Docker and Python
trallard
47
3.9k
Mind Mapping
helmedeiros
PRO
1
280
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Transcript
ActiveModelSerializersについて Kobayashi Yuta
ActiveModelSerializersとは? その名の通り、ActiveModelをJsonにSerializeできるgemです。 似た⽤途のgemとしてはRails標準のjbuilder等があります。
ActiveModelSerializersを使う理由 主に以下の理由から採⽤しました。 Rubyに則った記法ができるため、直感的に理解できる レスポンスの速度が早い ⽇本語の記事が多い
本題に⼊る前に… Railsではsnake_caseを利⽤するのが⼀般的ですが、今回はフロントエンドの変数の書式に合 わせるために、lowerCamelCaseでjsonが出⼒されるように設定しています。 # active_model_serializers.rb ActiveModelSerializers.config.key_transform = :camel_lower
基本的な使い⽅ # suppliers_controller.rb def show supplier = Supplier.find_by(id: params[:id]) render
status: 200, json: supplier, serializer: SupplierSerializer end # supplier_serializer.rb class SupplierSerializer < ActiveModel::Serializer attributes :id, :name, :tel, :supplier_code end
Response { "supplier": { "id":1, "name":" サプライヤーA", "tel":"090-1234-5678", "supplierCode": "123456789"
} }
Serializerの種類 # suppliers_controller.rb def index suppliers = Supplier.all render status:
200, json: suppliers, each_serializer: SupplierSerializer end 先程と同じSerializerモデルを利⽤していますが、こちらの例ではeach_serializerでserializeを ⾏っています。 指定しなくても⾃動で最適なSerializerを利⽤してくれますが、明⽰的に指定することにより 依存関係をわかりやすくしています。
Response { "suppliers": [ { "id":1, "name":" サプライヤーA", "tel":"090-1234-5678", "supplierCode":
"123456789" }, { "id":2, "name":" サプライヤーB", "tel":"090-1234-5678", "supplierCode": "123456789" } ] }
少し凝ったjsonの作り⽅ # supplier_serializer.rb class SupplierSerializer < ActiveModel::Serializer attributes :id, :name,
:tel, :supplier_code, :published_at # 出⼒したいproperty 名と違う時はkey で指定する attribute :image_file, key: :image # 出⼒結果に含めるかどうかをif: -> で条件付けする attribute :main_image, if: -> { object.main_image.present? } # method の呼び出し結果をresponse に含む def published_at I18n.l(object.published_at, format: "%Y/%m/%d %H:%M") end # Model が持つAssociation を含む has_one :category, serializer: CategorySerializer, if: -> { object.category.present? } has_many :supplier_labels, serializer: SupplierLabelSerializer end
ご静聴ありがとうございました