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
Rails 7.1 の新機能が使いたくて調べてみた
Search
Sho Ezawa
January 19, 2024
Programming
1
410
Rails 7.1 の新機能が使いたくて調べてみた
「【Techouse × Leaner共同開催】Ruby / Rails勉強会」での登壇資料
https://techouse.connpass.com/event/305572/
Sho Ezawa
January 19, 2024
Tweet
Share
More Decks by Sho Ezawa
See All by Sho Ezawa
デザインエンジニア?を模索している話
glico800
1
73
詳細度調整擬似クラスの使い所を考えてみた
glico800
1
1.1k
なぜスタートアップで部活をやるのか
glico800
1
480
Figmaで作る動くペーパープロトタイプ
glico800
0
730
弱いパスワードの作り方
glico800
1
180
Other Decks in Programming
See All in Programming
CSC307 Lecture 03
javiergs
PRO
1
470
20251212 AI 時代的 Legacy Code 營救術 2025 WebConf
mouson
0
240
[AtCoder Conference 2025] LLMを使った業務AHCの上⼿な解き⽅
terryu16
6
1k
JETLS.jl ─ A New Language Server for Julia
abap34
2
470
公共交通オープンデータ × モバイルUX 複雑な運行情報を 『直感』に変換する技術
tinykitten
PRO
0
180
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
440
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
600
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
180
はじめてのカスタムエージェント【GitHub Copilot Agent Mode編】
satoshi256kbyte
0
150
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
120
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
1
310
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
620
Featured
See All Featured
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
420
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
40
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
74
YesSQL, Process and Tooling at Scale
rocio
174
15k
[RailsConf 2023] Rails as a piece of cake
palkan
58
6.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.7k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
270
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
150
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
180
AI Search: Where Are We & What Can We Do About It?
aleyda
0
6.8k
What the history of the web can teach us about the future of AI
inesmontani
PRO
0
390
Transcript
Rails 7.1 の新機能が使いた くて調べてみた 【Techouse × Leaner共同開催】Ruby / Rails勉強会 2024/01/19
Fri. @glico800
登壇者情報 @glico800 Leaner Technologies Inc. デザインエンジニア フロントエンドの実装が中心 バックエンドも勉強中 TypeScript, React,
Ruby, Ruby on Rails 動物好き
ちょっと会社の話(1/2) Leaner Technologies Inc. は 調達DXの会社 Leaner見積とLeaner購買という2つのプロダクト
ちょっと会社の話(2/2) 私はLeaner購買の開発チーム Leaner購買は Rails 7.1.1
せっかくチームがアップデートを頑張ってくれているので Rails 7.1 の新機能が 使いたい!!
もくじ 1. ActiveRecord::Base.normalizes 2. 汎用の非同期クエリを対象とするActive Record API 3. まとめ 個人的に業務に使いそうな機能をピックアップ
1. ActiveRecord::Base.normalizes
ActiveRecord::Base.normalizes で正規化 before after 1 class User < ActiveRecord::Base 2
before_validation :normalize_email 3 4 private 5 6 def normalize_email 7 email.strip.downcase 8 end 9 end 1 class User < ActiveRecord::Base 2 normalizes :email, with: -> email { email.strip.downcase } 3 end
正規化のタイミング (1/3) attribute の assign/update 1 user = User.find(1) 2
user.email = "
[email protected]
\n" 3 user.email # => "
[email protected]
" 4 5 user.update(email: "
[email protected]
\n") 6 user.email # => "
[email protected]
"
正規化のタイミング (2/3) ActiveRecord::FinderMethods 1 User.find_by(email: "
[email protected]
").count # => 1 2
3 # キーワード引数には適用される 4 User.find_by(email: "\
[email protected]
").count # => 1 5 User.where(email: "\
[email protected]
").count # => 1 6 User.exists?(email: "\
[email protected]
") # => true 7 8 # プレースホルダには適用されない 9 User.where(["email = ?", "\
[email protected]
"]).count # => 0 10 User.exists?(["email = ?", "\
[email protected]
"]) # => false
正規化のタイミング (3/3) 明示的に呼ぶ Model.normalize_value_for から呼ぶことも可 see: activerecord/lib/active_record/normalization.rb 1 # normalizes
の追加前に保存された値は正規化されない 2 legacy_user = User.find(1) 3 legacy_user.email # => "
[email protected]
\n" 4 5 # 明示的に呼ぶことで正規化 6 legacy_user.normalize_attribute(:email) 7 legacy_user.email # => "
[email protected]
" 8 legacy_user.save 1 User.normalize_value_for(:email, "
[email protected]
\n")
nil の扱い apply_to_nil オプションを付けると nil のときも正規化される nil のときは正規化は適用されないのでエラーにはならない 1 class
User < ActiveRecord::Base 2 # デフォルトでは nil 考慮は不要 3 normalizes :email, with: -> email { email.strip.downcase } 4 end 3 normalizes :email, with: -> email { email&.strip&.downcase }, apply_to_nil: true 1 class User < ActiveRecord::Base 2 # nil の考慮が必要になる 4 end
2. 汎用の非同期クエリを対象とする Active Record API
非同期クエリがサポート拡張 集計メソッド async_count , async_count_by_sql async_minimum , async_maximum async_sum async_average
検索メソッド async_pick async_find_by_sql async_pluck async_ids
使い方 非同期なカウント 同期的なカウント 1 published_count = Post.where(published: true).count # =>
10 2 3 # 他の処理たち... 4 5 published_count 1 # <ActiveRecord::Promise status=pending> を返す 2 promise = Post.where(published: true).async_count 3 4 # 他の処理たち... 5 6 promise.value # => 10
load_async との違い 逆に ActiveRecord::Relation を返さないメソッドでは load_async は使えない ActiveRecord::Relation を返すメソッドでは load_async
が使える 1 orders = Order.where(user_id: user.id).load_async 2 requisitions = user.requisitions.approved.load_async 1 # NG => undefined method `load_async' for ... 2 ng_count = Order.shipped.count.load_async 3 ng_requisition_ids = Requisition.approved.pluck(:id).load_async 4 5 # OK 6 count = Order.shipped.async_count 7 requisition_ids = Requisition.approved.async_pluck(:id)
config/application.rb 1. async_query_executor これを設定しないと非同期クエリが流れない 2. global_executor_concurrency async_query_executor が :global_thread_pool のときの並行実行数上限
上限アップの際はサーバーリソースと要相談 1 # 設定値は :global_thread_pool or :multi_thread_pool ( 初期値: nil) 2 config.active_record.async_query_executor = :global_thread_pool 1 # ( 初期値: 4) 2 config.active_record.global_executor_concurrency = 5
どんなときに使いそうか データ分析機能 重いクエリを複数実行する ActiveRecord::Relation を返さないメソッドが多い 集計期間が長い集計機能ほど恩恵が大きい 例:Leaner見積の年間コストインパクト等の算出 (↑ 開発合宿で実際にパフォーマンス向上に成功していた) 今回の非同期クエリの実装者は
Shopify のエンジニアさん (Shopify でも使っているのかも)
まとめ
Rails 7.1 でよく使いそうな新機能 ActiveRecord::Base.normalizes での正規化 before_validation を使って書くより簡単 キーワード引数以外の ActiveRecord::FinderMethods は正規化されない
normalizes の追加前に保存された値は正規化されない 汎用の非同期クエリを対象とする Active Record API load_async ではできなかった集計処理が非同期化できる async_query_executor の設定を忘れずに データ分析機能で活躍しそう
一緒に働くエンジニアを募 集しています! Leaner のプロダクトに興味があれば ぜひお声掛けください!🍖
おわり