Ruby/Rails アップグレード記録 - Phase 2

概要

項目 Before After
Rails 7.2.3 8.0.4

対応した問題

1. Gemfile の更新

対応:

# Before
gem 'rails'

# After
gem 'rails', '~> 8.0.0'

その後 bundle update rails を実行。


2. ルーティングの複数パス指定の廃止 (Rails 8.1で削除予定)

警告:

DEPRECATION WARNING: Mapping a route with multiple paths is deprecated and will be removed in Rails 8.1.

原因: get :following, :followers のように1つのメソッド呼び出しで複数のアクションをマッピングする書き方が非推奨になった。

対応:
```ruby

Before (config/routes.rb)

resources :brothers, except: [:destroy] do
member do
get :following, :followers
end
end

After

resources :brothers, except: [:destroy] do
member do
get :following
get :followers
end
end
```


3. to_time のタイムゾーン挙動変更 (Rails 8.1で変更予定)

警告:

DEPRECATION WARNING: `to_time` will always preserve the full timezone rather than offset of the receiver in Rails 8.1.

原因: Rails 8.1で to_time メソッドがタイムゾーン情報を完全に保持するようになる。

対応:
```ruby

config/application.rb に追加

config.active_support.to_time_preserves_timezone = :zone
```


4. http_basic_authenticate_with の引数バリデーション強化 (Rails 8.0)

エラー:

ArgumentError: Expected name: to be a String, got NilClass

原因: Rails 8.0では http_basic_authenticate_withname: 引数がクラスロード時にバリデーションされるようになった。環境変数が未設定の場合に nil が渡されるとエラーになる。

対応:
```ruby

Before

class Tent::BaseController < ApplicationController
http_basic_authenticate_with name: ENV['BASIC_AUTH_USERNAME'],
password: ENV['BASIC_AUTH_PASSWORD'] if Rails.env.production?
end

After

class Tent::BaseController < ApplicationController
if Rails.env.production? && ENV['BASIC_AUTH_USERNAME'].present?
http_basic_authenticate_with name: ENV['BASIC_AUTH_USERNAME'],
password: ENV['BASIC_AUTH_PASSWORD']
end
end
```

ポイント: 条件を if の前に移動し、環境変数の存在チェックを追加する。


まとめ

Rails 8.0 の主な変更点

変更 影響
ルーティングの複数パス非推奨 個別のメソッド呼び出しに分割
to_time タイムゾーン挙動 設定で新しい挙動をオプトイン
http_basic_authenticate_with バリデーション強化 引数が nil の場合エラー

Rails 8.0 の特徴

  • Rails 7.2からの移行は比較的スムーズ
  • 破壊的変更は少なく、主に非推奨警告への対応
  • Rails 8.1に向けた準備として設定を追加しておくとよい

教訓

  • 非推奨警告は先に対応する - 次のバージョンアップがスムーズになる
  • テストを頻繁に実行する - 変更のたびにテストを実行して問題を早期発見