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
84
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
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
Road to RubyKaigi: Making Tinny Chiptunes with Ruby
makicamel
4
530
VitestのIn-Source Testingが便利
taro28
8
2.4k
「理解」を重視したAI活用開発
fast_doctor
0
270
Ruby on Railroad: The Power of Visualizing CFG
ydah
0
290
プロフェッショナルとしての成長「問題の深掘り」が導く真のスキルアップ / issue-analysis-and-skill-up
minodriven
8
1.9k
音声プラットフォームのアーキテクチャ変遷から学ぶ、クラウドネイティブなバッチ処理 (20250422_CNDS2025_Batch_Architecture)
thousanda
0
380
RubyKaigi Dev Meeting 2025
tenderlove
1
1.2k
エンジニア向けCursor勉強会 @ SmartHR
yukisnow1823
3
12k
AI時代の開発者評価について
ayumuu
0
230
Enterprise Web App. Development (1): Build Tool Training Ver. 5
knakagawa
1
120
Qiita Bash
mercury_dev0517
2
220
実践Webフロントパフォーマンスチューニング
cp20
45
9.9k
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
5.5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
A Tale of Four Properties
chriscoyier
158
23k
Docker and Python
trallard
44
3.4k
Bash Introduction
62gerente
611
210k
How to Think Like a Performance Engineer
csswizardry
23
1.6k
Fireside Chat
paigeccino
37
3.4k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
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