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を早めに動かす - プラットフォーム依存の問題は早期に発見できる