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.4k
Rails 6 Upgrade "Practical" Guide
megane42
January 30, 2020
Tweet
Share
More Decks by megane42
See All by megane42
Immutable ActiveRecord
megane42
0
250
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
660
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
97
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
230
How to Make "DJ giftee"
megane42
1
930
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
600
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
260
Other Decks in Programming
See All in Programming
例外処理を理解して、設計段階からエラーを見つけやすく、起こりにくく #phpconfuk
kajitack
12
6.2k
CSC509 Lecture 13
javiergs
PRO
0
250
歴史から学ぶ「Why PHP?」 PHPを書く理由を改めて理解する / Learning from History: “Why PHP?” Rediscovering the Reasons for Writing PHP
seike460
PRO
0
160
複数チーム並行開発下でのコード移行アプローチ ~手動 Codemod から「生成AI 活用」への進化
andpad
0
170
Feature Flags Suck! - KubeCon Atlanta 2025
phodgson
0
140
2026年向け会社紹介資料
misu
0
240
All(?) About Point Sets
hole
0
180
ゼロダウンタイムでミドルウェアの バージョンアップを実現した手法と課題
wind111
0
200
CloudNative Days Winter 2025: 一週間で作る低レイヤコンテナランタイム
ternbusty
7
1.5k
DartASTとその活用
sotaatos
2
140
自動テストを活かすためのテスト分析・テスト設計の進め方/JaSST25 Shikoku
goyoki
3
700
Reactive Thinking with Signals and the new Resource API
manfredsteyer
PRO
0
100
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Documentation Writing (for coders)
carmenintech
76
5.1k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.3k
YesSQL, Process and Tooling at Scale
rocio
174
15k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
658
61k
[RailsConf 2023] Rails as a piece of cake
palkan
57
6.1k
4 Signs Your Business is Dying
shpigford
186
22k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
670
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.8k
Designing Experiences People Love
moore
142
24k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
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