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
330
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
66
詳細度調整擬似クラスの使い所を考えてみた
glico800
1
1.1k
なぜスタートアップで部活をやるのか
glico800
0
470
Figmaで作る動くペーパープロトタイプ
glico800
0
630
弱いパスワードの作り方
glico800
1
170
Other Decks in Programming
See All in Programming
Result型で“失敗”を型にするPHPコードの書き方
kajitack
5
580
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
700
#kanrk08 / 公開版 PicoRubyとマイコンでの自作トレーニング計測装置を用いたワークアウトの理想と現実
bash0c7
1
670
NPOでのDevinの活用
codeforeveryone
0
720
RailsGirls IZUMO スポンサーLT
16bitidol
0
140
XP, Testing and ninja testing
m_seki
3
220
地方に住むエンジニアの残酷な現実とキャリア論
ichimichi
5
1.5k
5つのアンチパターンから学ぶLT設計
narihara
1
150
Deep Dive into ~/.claude/projects
hiragram
11
2.3k
GraphRAGの仕組みまるわかり
tosuri13
8
520
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
710
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
550
Featured
See All Featured
Six Lessons from altMBA
skipperchong
28
3.9k
What's in a price? How to price your products and services
michaelherold
246
12k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Mobile First: as difficult as doing things right
swwweet
223
9.7k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
Gamification - CAS2011
davidbonilla
81
5.3k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
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 のプロダクトに興味があれば ぜひお声掛けください!🍖
おわり