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
44
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
470
GitHubのコマンドパレット使ってますか?
yuki21
0
1.7k
キャッシュを利用してRailsアプリの処理を高速化する
yuki21
0
130
Next.js & ElectronでTodoアプリを作る
yuki21
0
740
gRPCを完璧に理解する
yuki21
0
52
RSpec -基本の基-
yuki21
0
58
Committeeを導入してみた
yuki21
0
150
マイクロサービスとモノリスとKBR
yuki21
0
54
脆弱性について
yuki21
0
170
Other Decks in Programming
See All in Programming
RTSPクライアントを自作してみた話
simotin13
0
610
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
150
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
710
CSC307 Lecture 17
javiergs
PRO
0
320
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
360
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
6.6k
C# and C++ Interoperability - cho-dotnetnew
harukasao
0
190
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.6k
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
340
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
11
4.3k
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
180
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
550
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The Cult of Friendly URLs
andyhume
79
6.9k
BBQ
matthewcrist
89
10k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Designing for Timeless Needs
cassininazir
1
260
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
340
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
250
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
950
How to train your dragon (web standard)
notwaldorf
97
6.7k
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
ご静聴ありがとうございました