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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
yuki21
July 17, 2020
Programming
49
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
490
GitHubのコマンドパレット使ってますか?
yuki21
0
1.7k
キャッシュを利用してRailsアプリの処理を高速化する
yuki21
0
130
Next.js & ElectronでTodoアプリを作る
yuki21
0
740
gRPCを完璧に理解する
yuki21
0
59
RSpec -基本の基-
yuki21
0
62
Committeeを導入してみた
yuki21
0
170
マイクロサービスとモノリスとKBR
yuki21
0
60
脆弱性について
yuki21
0
170
Other Decks in Programming
See All in Programming
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.7k
仕様駆動開発の消費期限
watany
20
8.9k
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.6k
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
320
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
160
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
250
Oxlintはいいぞ(続)
yug1224
1
400
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
1
250
AWS Transform Customによる Spring Boot 2.xから4.xへのVerUp
satoshi256kbyte
2
100
運用ダッシュボードの設計を誰も教えてくれないのだけどみなさんどうしてるんですか? - チームに監視するという文化を根付かせるための第一歩を踏みたい -
satoshi256kbyte
1
140
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
170
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
280
Featured
See All Featured
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
220
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
900
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Ruling the World: When Life Gets Gamed
codingconduct
0
310
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
First, design no harm
axbom
PRO
2
1.3k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
How to make the Groovebox
asonas
2
2.3k
Statistics for Hackers
jakevdp
799
230k
Designing for Performance
lara
611
70k
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
ご静聴ありがとうございました