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
290
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
690
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
100
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
240
How to Make "DJ giftee"
megane42
1
950
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
Automatic Grammar Agreementと Markdown Extended Attributes について
kishikawakatsumi
0
180
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
組織で育むオブザーバビリティ
ryota_hnk
0
160
Apache Iceberg V3 and migration to V3
tomtanaka
0
120
CSC307 Lecture 07
javiergs
PRO
0
530
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
220
今から始めるClaude Code超入門
448jp
5
6.6k
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
1k
開発者から情シスまで - 多様なユーザー層に届けるAPI提供戦略 / Postman API Night Okinawa 2026 Winter
tasshi
0
180
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
120
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
130
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
100k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
230
GraphQLの誤解/rethinking-graphql
sonatard
74
11k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Six Lessons from altMBA
skipperchong
29
4.1k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
61
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
220
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
420
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