はじめまして。じぶんは日本の豊かなブログサービスです。
ブラザーに 今すぐなる

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

概要

項目 Before After
Rails 8.0.4 8.1.1

対応した問題

1. Gemfile の更新

対応:
```ruby

Before

gem 'rails', '~> 8.0.0'

After

gem 'rails', '~> 8.1.0'
```

その後 bundle update rails を実行。


2. to_time_preserves_timezone 設定の削除

警告:

DEPRECATION WARNING: `config.active_support.to_time_preserves_timezone` is deprecated and will be removed in Rails 8.2

原因: Rails 8.1で to_time がタイムゾーンを保持する挙動がデフォルトになった。そのため設定は不要になり、Rails 8.2で削除予定。

対応:
```ruby

Before (config/application.rb)

config.active_support.to_time_preserves_timezone = :zone

After

削除(Rails 8.1ではデフォルト動作)


---

## まとめ

### Rails 8.1 の主な変更点
| 変更 | 影響 |
|------|------|
| `to_time_preserves_timezone` がデフォルト化 | 設定を削除 |

### Rails 8.1 の特徴
- Rails 8.0からの移行は非常にスムーズ
- 破壊的変更はほとんどなし
- Rails 8.0で追加した互換性設定を削除するだけ

### 教訓
- **段階的アップグレードの効果** - 8.0で先に対応していたため8.1への移行がスムーズだった
- **非推奨設定の確認** - 新しいバージョンでデフォルト化された設定は削除する

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に向けた準備として設定を追加しておくとよい

教訓

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

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

概要

項目 Before After
Ruby 3.1.0 3.4.7
Rails 7.0.1 7.2.3

対応した問題

1. RSpec の fixture_path 設定変更 (Rails 7.2)

エラー:

NoMethodError: undefined method `fixture_path=' for #<RSpec::Core::Configuration>

原因: Rails 7.2で fixture_path=fixture_paths= に変更された。

対応:
```ruby

Before

config.fixture_path = #{::Rails.root}/spec/fixtures

After

config.fixture_paths = [#{::Rails.root}/spec/fixtures]
```


2. ActiveSupport::Deprecation の変更 (Rails 7.2)

エラー:

private method 'warn' called for class ActiveSupport::Deprecation

原因: Rails 7.2で ActiveSupport::Deprecation.warn がクラスメソッドからインスタンスメソッドに変更された。古いgemがクラスメソッドとして呼び出していた。

対応: 初期化ファイルでモンキーパッチを追加
```ruby

config/initializers/deprecation_fix.rb

if Rails.gem_version >= Gem::Version.new(7.2)
module ActiveSupport
class Deprecation
class << self
def warn(message = nil, callstack = nil)
ActiveSupport::Deprecation.new.warn(message, callstack)
end

    def silence(&block)
      ActiveSupport::Deprecation.new.silence(&block)
    end
  end
end

end
end
```


3. Compass/Compass-Rails の削除 (Ruby 3.4)

エラー:

NoMethodError: undefined method 'exists?' for class File

原因: File.exists? はRuby 3.2で削除された。Compass gemが古いAPIを使用していた。

対応:
- compass-rails gemをGemfileから削除
- Compassが提供していたmixinを自前で実装

// _import.css.scss に追加
@mixin box-sizing($type: border-box) {
  box-sizing: $type;
}

@mixin clearfix {
  &::after {
    content: "";
    display: table;
    clear: both;
  }
}

@mixin border-radius($radius) {
  border-radius: $radius;
}

@mixin transition-property($property) {
  transition-property: $property;
}

@mixin transition-duration($duration) {
  transition-duration: $duration;
}

@mixin transition-timing-function($timing) {
  transition-timing-function: $timing;
}

@mixin background-size($size) {
  background-size: $size;
}

@mixin display-box {
  display: flex;
}

@mixin box-pack($pack) {
  @if $pack == center {
    justify-content: center;
  }
}

@mixin box-align($align) {
  @if $align == center {
    align-items: center;
  }
}

SCSSファイルからのCompassインポート削除:
```scss
// Before
@import 'compass';
@import 'compass/css3/box';
@import 'compass/reset';

// After
// 削除(代わりに自前のmixinを使用)
```


4. Puma設定の更新 (Puma 7.x)

エラー:

NameError: uninitialized constant Puma::DSL::DefaultRackup

原因: DefaultRackup は古いPuma設定で、Puma 7.xでは削除された。

対応:
```ruby

Before (config/puma.rb)

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'

After

max_threads_count = ENV.fetch(RAILS_MAX_THREADS) { 5 }
min_threads_count = ENV.fetch(RAILS_MIN_THREADS) { max_threads_count }
threads min_threads_count, max_threads_count

worker_timeout 3600 if ENV.fetch(RAILS_ENV, development) == development

port ENV.fetch(PORT) { 3000 }
environment ENV.fetch(RAILS_ENV) { development }
pidfile ENV.fetch(PIDFILE) { tmp/pids/server.pid }
plugin :tmp_restart
```


5. GitHub Actions のプラットフォーム対応

エラー:

Your bundle only supports platforms ["arm64-darwin-23"] but your local platform
is x86_64-linux.

原因: MacでGemfile.lockを生成したため、Linuxプラットフォームが含まれていなかった。

対応:
bash
bundle lock --add-platform x86_64-linux


6. データベース設定の改善

変更: Docker環境とローカル環境の両方で動作するように設定を変更

# config/database.yml
default: &default
  adapter: postgresql
  encoding: utf8
  host: <%= ENV.fetch("DB_HOST") { "localhost" } %>
  username: postgres
  password:
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

GitHub Actions 設定

name: Ruby

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: password
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    env:
      RAILS_ENV: test
      PGHOST: localhost
      PGUSER: postgres
      PGPASSWORD: password

    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.4.7'
          bundler-cache: true
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      - run: npm install
      - run: sudo apt-get -yqq install libpq-dev
      - run: |
          bundle exec rails db:create
          bundle exec rails db:migrate
      - run: bundle exec rspec

まとめ

Rails 7.2 の主な変更点

変更 影響
fixture_pathfixture_paths RSpec設定の修正が必要
ActiveSupport::Deprecation API変更 クラスメソッドからインスタンスメソッドに
Puma 7.x DefaultRackup 削除、設定形式の変更

Ruby 3.4 の主な変更点

変更 影響
File.exists? 削除 Compassなど古いgemが動作しない

教訓

  • メンテナンスされていないgemは早めに置き換える - Compassのような古いgemは、Rubyのメジャーバージョンアップ時に問題になりやすい
  • 段階的にアップグレードする - 一度に大きくバージョンを上げると問題の切り分けが難しくなる
  • CIを早めに動かす - プラットフォーム依存の問題は早期に発見できる

まめぶろのRuby/Rails アップグレード記録

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

  1. Rails 7.0.1 → 7.2.3 , Ruby 3.1.0 → 3.4.7
    https://mameblo.com/entries/10067

  2. Rails 7.2.3 → 8.0.4
    https://mameblo.com/entries/10068

  3. Rails 8.0.4 → 8.1.1
    https://mameblo.com/entries/10069

  4. Ruby 4

所感など

思い出した

ブラザー元気かい?

Loading...