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 6 Upgrade "Practical" Guide
Search
megane42
January 30, 2020
Programming
6
1.3k
Rails 6 Upgrade "Practical" Guide
megane42
January 30, 2020
Tweet
Share
More Decks by megane42
See All by megane42
Immutable ActiveRecord
megane42
0
140
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
520
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
77
ゆる計算理論ラジオ / P vs NP for beginner
megane42
0
180
How to Make "DJ giftee"
megane42
1
840
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
510
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
160
Other Decks in Programming
See All in Programming
SpringBoot3.4の構造化ログ #kanjava
irof
2
990
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
190
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
Unity Android XR入門
sakutama_11
0
160
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
560
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
110
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
780
Grafana Cloudとソラカメ
devoc
0
170
Honoとフロントエンドの 型安全性について
yodaka
7
1.2k
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
570
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
120
SwiftUI Viewの責務分離
elmetal
PRO
1
230
Featured
See All Featured
Done Done
chrislema
182
16k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
410
Visualization
eitanlees
146
15k
Facilitating Awesome Meetings
lara
52
6.2k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
It's Worth the Effort
3n
184
28k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Practical Orchestrator
shlominoach
186
10k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
174
51k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Transcript
Rails 6 Upgrade "Practical" Guide megane42 / Hikaru Kazama @
giftee 2020/01/30 gotanda.rb #41 @ Mobile Factory
自己紹介 • @megane42 • Rails 歴 1 年半 • giftee
• 趣味 : かっこいい ワンタイムパスワード集め
ちょっと宣伝 • 個人開発 NIGHT #3 やります! • 2020/02/06(木) 19:30 -
• giftee @ 東五反田 • https://giftee.connpass.com/event/ 164484/ • 社内有線を作った話をします(予定)
アジェンダ • 実践的な更新手順 • 変更点 25 本ノック • 5.2.4 ->
6.0.2 • キャッチーなやつだけ紹介していきます
実践的な更新手順
基本的な流れ • https://qiita.com/jnchito/items/ 0ee47108972a0e302caf/
感想 「カバレッジが高いからといって…」 と思っていた時期が私にもありました (カバレッジ 69%) カバレッジが大正義
仕方ないので・・・ https://edgeguides.rubyonrails.org/ 6_0_release_notes.html リリースノートを地道に読んだ
リリースノートを読むコツ • リリースノートに対応する PR を見に行く • テストコードを読む • メソッドの前の doc
コメントを読む • その PR に含まれる CHANGELOG を見る (サンプルコード付きで説明が載っていると きがある)
リリースノートを読むコツ • すべてを理解しようとしない • Removals, Deprecations に関しては、 git grep してヒットしなければ深追いしない
• 自動テスト、手動テストを信じて次に進む • 後日、念のため `DEPRECATION WARNING` で ログを検索するといいかもしれない
共有 • 今後他のチームの役に立つかなと思って Slack チャンネルを作ってメモを残した
変更点 25本ノック
ActionDispatch:: HostAuthorization • DNS リバインディング対策のために、新しい rack middleware が導入された • DNS
リバインディングとは : https://qiita.com/kidach1/ items/1a3d66abb80bb67e0d64 • ユーザーに罠サイトを表示させたあと、罠サイトのドメ インの A レコードを攻撃対象サイトの IP に向け変える ことで、罠サイトの JS を攻撃対象サイトに向けて実行さ せる 1/25
ActionDispatch:: HostAuthorization • ホスト名のホワイトリストを Rails.application.config.hosts に追加できる • development では全 IP
アドレスと localhost がデフォルトで許可済み • 他の環境ではデフォルトで未設定 = 全許可 なので、取り急ぎ作業は不要 1/25
rails routes —expanded • `-g キーワード` で絞り込みも可能 root@670934a4e388:/app# rails routes
--expanded -g user --[ Route 1 ]---------------------------- Prefix | new_admin_user_session Verb | GET URI | /admins/sign_in(.:format) Controller#Action | admins/sessions#new --[ Route 2 ]---------------------------- Prefix | admin_user_session Verb | POST URI | /admins/sign_in(.:format) Controller#Action | admins/sessions#create 2/25
rails db:prepare • 新コマンド • データベースが無かったら db:setup する • データベースがあったら
db:migration する • bin/setup でも早速使われている 3/25
rails db:seed:replant • 新コマンド • テーブルからデータだけを消しつつ、 seed を再度流し込んでくれる • 従来の方法
(rails db:setup db:seed) だと、 テーブル構造まで作り直しになってしまう 4/25
ActionDispatch::Request:: Session#dig session[:user] = { id: 1, icon: “http://example.org/nyancat.jpg” }
# ͜Ε·Ͱ session[:user][:icon] # ࠓ͔Β͜͏ॻ͚Δ session.dig(:user, :icon) • session を dig できるようになった 5/25
ActiveRecord::Relation #extract_associated • スコープで絞り込みつつ、その関連だけを 取ってこれる Post.active.extract_associated(:comments) => [ #<Comment id:
1, content: "foo", post_id: 1, …>, #<Comment id: 2, content: "bar", post_id: 1, …>, #<Comment id: 3, content: "baz", post_id: 2, …> ... ] 6/25
ActiveRecord::Relation #annotate • なんかの調査時に役立ちそう Post.annotate("this is a comment”) .where(id: 123)
.to_sql # => SELECT “posts”.* FROM “posts" WHERE "posts"."id" = 123 /* this is a comment */ 7/25
ActiveRecord::Relation #destroy_by / #delete_by • ありそうでなかった # ͜Ε·Ͱ Post.where(id: [1,2]).destroy_all
# ࠓ͔Β͜͏ॻ͚Δ Post.destroy_by(id: [1,2]) 8/25
ActiveRecord::Relation #pick • ユースケースがピンポイントすぎるw # ͜Ε·Ͱ Person.where(id: 1).limit(1).pluck(:name).first # ࠓ͔Β͜͏ॻ͚Δ
Person.where(id: 1).pick(:name) 9/25
ActiveRecord::Base .create_or_find_by/! • find_or_create_by と違って、レースコン ディション発生時にレコードが 2 つできない (DB にユニーク制約がかかっている前提)
find したけど < 見つからないから > create してヨシ! 図. find_or_create_by 10/25
update_attributes/! の廃止 • update または update! を使いましょう 11/25
insert_all(!) / upsert_all • `activerecord-import` gem と違って、 ActiveRecord オブジェクトを生成しない •
(+) より早い • (-) バリデーションやコールバックが実行さ れない 12/25
Enum から negative scopes が生えた • `not_xxx` が使えるようになった class Post
< ActiveRecord::Base enum status: %i[ drafted active trashed ] end # ͜Ε·Ͱ Post.where.not(status: :drafted) # ࠓ͔Β͜͏ॻ͚Δ Post.not_drafted 13/25
where の中で endless range を使えるようになった • 生々しいクエリを書かなくて済む # ͜Ε·Ͱ Post.where(“id
> ?”, 42) # ࠓ͔Β͜͏ॻ͚Δ Post.where(id: 42..) 14/25
クエリ結果の暗黙的な並び順 を設定できるようになった • `first` や `last` が何のカラムをベースに動く かを設定できるようになった。 なるほどなぁ〜 class
Project < ActiveRecord::Base self.implicit_order_column = "created_at" end 15/25
マイグレーションで同名カラムを 作るとエラーになるようになった • エラーにならなかったのか・・・ 16/25
子がユニークじゃないときに 親を save したときの挙動が変わった • 下記を実行するとどうなるでしょう? (parent has_many children とする)
parent = Parent.new(children: [ Child.new(name: ‘Wiske'), Child.new(name: ‘Wiske') ]) parent.save ユニーク制約違反 17/25
子がユニークじゃないときに 親を save したときの挙動が変わった • 5 以前 : 親と最初の子までは save
される • 6 以降 : 親も子も save されない parent = Parent.new(children: [ Child.new(name: ‘Wiske'), Child.new(name: ‘Wiske') ]) parent.save ユニーク制約違反 17/25
Date, DateTime, Time, TimeWithZone #before? / #after? • 不等号が辛くなったら思い出してください #
͜Ε·Ͱ Date.new(2020, 1, 30) < Date.new(2020, 1, 31) # ࠓ͔Β͜͏ॻ͚Δ Date.new(2020, 1, 30).before?(Date.new(2020, 1, 31)) 18/25
Enumerable#index_with • これが役立つときに思い出せるかどうか… post = Post.new(title: "hey", body: "what's up?")
[:title, :body].index_with do |attr| post.public_send(attr) End # => { title: "hey", body: "what's up?" } 19/25
Hash# deep_transform_values(!) • transform_values(!) は ruby にあるが、 ネストしていると意図した結果にならない hash =
{a: 1, b: {c: 2} } hash.transform_values { |v| v.to_s } # => {:a=>"1", :b=>"{:c=>2}"} hash.deep_transform_values { |v| v.to_s } # =>{:a=>"1", :b=>{:c=>"2"}} 20/25
credentials.yml.enc • 長くなったのでまとめました https://qiita.com/megane42/items/ 539812678773c60b7c8a 21/25
その他 : ActiveRecord のもろもろ • ActiveRecord 周りの変更 3 点について kamipo
さん自ら解説した記事 https://blog.kamipo.net/entry/2019/05/ 15/152652 22/25
その他 : メジャーどころ • Webpacker 標準化 • ActionText • Zeitwerk
(a.k.a. Z-loader) • https://qiita.com/alfa/items/3a432c31346a705d0690 • ちなみに Rails 6.0.2 だと `rails zeitwerk:check` がコケます 25/25
None
Rails 6 Upgrade "Practical" Guide megane42 / Hikaru Kazama @
giftee 2020/01/30 gotanda.rb #41 @ Mobile Factory