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
160
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
570
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
83
ゆる計算理論ラジオ / P vs NP for beginner
megane42
0
200
How to Make "DJ giftee"
megane42
1
870
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
530
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
170
Other Decks in Programming
See All in Programming
複雑なフォームの jotai 設計 / Designing jotai(state) for Complex Forms #layerx_frontend
izumin5210
4
1.1k
Ruby's Line Breaks
yui_knk
2
1.2k
[NG India] Event-Based State Management with NgRx SignalStore
markostanimirovic
1
160
Java 24まとめ / Java 24 summary
kishida
3
500
Chrome Extension Techniques from Hell
moznion
1
160
AWSで雰囲気でつくる! VRChatの写真変換ピタゴラスイッチ
anatofuz
0
160
AI Coding Agent Enablement - エージェントを自走させよう
yukukotani
14
6.2k
PHPバージョンアップから始めるOSSコントリビュート / how2oss-contribute
dmnlk
1
1.1k
音声プラットフォームのアーキテクチャ変遷から学ぶ、クラウドネイティブなバッチ処理 (20250422_CNDS2025_Batch_Architecture)
thousanda
0
220
Do Dumb Things
mitsuhiko
0
440
リアルタイムレイトレーシング + ニューラルレンダリング簡単紹介 / Real-Time Ray Tracing & Neural Rendering: A Quick Introduction (2025)
shocker_0x15
1
310
PHPで書いたAPIをGoに書き換えてみた 〜パフォーマンス改善の可能性を探る実験レポート〜
koguuum
0
170
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Music & Morning Musume
bryan
47
6.5k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
13
680
The World Runs on Bad Software
bkeepers
PRO
67
11k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.1k
Become a Pro
speakerdeck
PRO
27
5.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
Side Projects
sachag
452
42k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
Facilitating Awesome Meetings
lara
54
6.3k
How GitHub (no longer) Works
holman
314
140k
Automating Front-end Workflow
addyosmani
1369
200k
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