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
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
53
RSpec -基本の基-
yuki21
0
59
Committeeを導入してみた
yuki21
0
160
マイクロサービスとモノリスとKBR
yuki21
0
55
脆弱性について
yuki21
0
170
Other Decks in Programming
See All in Programming
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.7k
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.6k
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
1
180
1B+ /day規模のログを管理する技術
broadleaf
0
120
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
190
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
110
AIで効率化できた業務・日常
ochtum
0
160
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.7k
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
570
スマートグラスで並列バイブコーディング
hyshu
0
280
symfony/aiとlaravel/boost
77web
0
120
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.6k
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Paper Plane
katiecoart
PRO
1
52k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
380
Side Projects
sachag
455
43k
New Earth Scene 8
popppiees
3
2.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.5k
Claude Code のすすめ
schroneko
67
230k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
340
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
230
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
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
ご静聴ありがとうございました