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
46
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
60
Committeeを導入してみた
yuki21
0
160
マイクロサービスとモノリスとKBR
yuki21
0
56
脆弱性について
yuki21
0
170
Other Decks in Programming
See All in Programming
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
420
Google Apps Script で Ruby を動かす
kawahara
0
120
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
230
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
160
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
210
音楽のための関数型プログラミング言語mimiumにおける多段階計算の活用
tomoyanonymous
1
380
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
2
250
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.2k
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
160
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
200
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
130
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.3k
Featured
See All Featured
Odyssey Design
rkendrick25
PRO
2
740
Between Models and Reality
mayunak
4
380
Bash Introduction
62gerente
615
220k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
510
Producing Creativity
orderedlist
PRO
348
40k
The Cult of Friendly URLs
andyhume
79
7k
From π to Pie charts
rasagy
0
240
[SF Ruby Conf 2025] Rails X
palkan
2
1.2k
Navigating Team Friction
lara
192
16k
Writing Fast Ruby
sferik
630
63k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
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
ご静聴ありがとうございました