LoginSignup
27
13

More than 3 years have passed since last update.

Docker + Rails(Ruby)をバージョンアップ(アップグレード)したお話

Last updated at Posted at 2021-02-13

Docker環境でRails(Ruby)アプリをアップグレードした記事がない!?

非Docker環境でRubyとRailsをアップグレードする方法はすぐに見つかりました。

伊藤さんが書かれてる下記の記事が本当にわかりやすいです。

永久保存版!?伊藤さん式・Railsアプリのアップグレード手順 - Qiita

ちなみに私はDocker Rails アップデートDocker Rails バージョンアップなどでググりましたが見つかりませんでした。ワードが違いますもんねw

「じゃあDocker環境でRubyとRailsをアップグレードする記事もすぐ見つかるでしょ」

そう思ったのですが

といういくつかのパターンがある印象を受けました。

それがこの投稿に至った理由です。

進め方、参考リンク

基本的には永久保存版!?伊藤さん式・Railsアプリのアップグレード手順 - Qiitaが全体のベースとして進めます。

後々同じことを繰り返さないために遭遇したWARNINGログやエラーログも書いていこうと思います。

読んで欲しい人

  • Docker + Railsアプリ作れた人(RSpecでテスト書いてあったら最高!!)
  • Docker + Ruby + Rails の環境をアップグレードしようとしている人

今回はRubyの公式からイメージを取得する方法で行なっています。
rbenvでRubyを設定、アップデートする方法は想定していません、ご了承ください(m_ _m)

動作環境

version
(アップグレード前)
version
(アップグレード後)
MacOS Mojave Mojave
Ruby 2.5.7 2.7.2
Rails 5.1.7 6.0.3.5
MySQL 5.7 5.7

注意点

あくまでアプリをアップグレードした私の備忘録です。完璧ではありません。
違う部分があればアドバイスいただけると助かります。

現在はサンプルアプリのリンクはありませんが、後々作成する予定です。

本題

手順1. 現在使用しているgemを最新にする

RubyとRailsアップグレードを進める前にDockerコンテナ内のgemをアップデートしておきます。
おなじみのコマンドですね。

shell
docker-compose run --rm web bundle update

私の場合はここで下記のログが表示されました。

shell
Ignoring bigdecimal-1.3.5 because its extensions are not built. Try: gem pristine bigdecimal --version 1.3.5
Ignoring bootsnap-1.5.1 because its extensions are not built. Try: gem pristine bootsnap --version 1.5.1
Ignoring mini_racer-0.3.1 because its extensions are not built. Try: gem pristine mini_racer --version 0.3.1
Ignoring msgpack-1.4.0 because its extensions are not built. Try: gem pristine msgpack --version 1.4.0
Ignoring msgpack-1.3.3 because its extensions are not built. Try: gem pristine msgpack --version 1.3.3
Ignoring puma-5.1.1 because its extensions are not built. Try: gem pristine puma --version 5.1.1
Ignoring websocket-driver-0.7.3 because its extensions are not built. Try: gem pristine websocket-driver --version 0.7.3
:
:

この後は通常のbundle installbundle updateと同じ表示がされていました。
これはリンクの通りに下記のコマンドを実行することでbundle installを実行しても上記のログも表示されなくなりました。

shell
docker-compose run --rm web gem pristine --all

手順2. 最新ではないgemを探して、できる限りバージョンアップする

docker-compose run --rm web bundle outdatedコマンドで最新バージョンを使っていないgemを確認できます。

shell
$ docker-compose run --rm web bundle outdated
Creating app_for_job_change_web_run ... done
Fetching https://github.com/thoughtbot/shoulda-matchers.git
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies................................................

Outdated gems included in the bundle:
  * actioncable (newest 6.1.1, installed 5.1.7)
  * actionmailer (newest 6.1.1, installed 5.1.7)
  * actionpack (newest 6.1.1, installed 5.1.7)
  * actionview (newest 6.1.1, installed 5.1.7)
  * activejob (newest 6.1.1, installed 5.1.7)
  * activemodel (newest 6.1.1, installed 5.1.7)
  * activerecord (newest 6.1.1, installed 5.1.7)
  * activesupport (newest 6.1.1, installed 5.1.7)
  * arel (newest 9.0.0, installed 8.0.0)
  * childprocess (newest 4.0.0, installed 3.0.0)
  * listen (newest 3.4.1, installed 3.1.5, requested >= 3.0.5, < 3.2) in groups "development"
  * polyamorous (newest 2.3.2, installed 2.3.0)
  * puma (newest 5.2.0, installed 3.12.6, requested ~> 3.7) in groups "default"
  * rails (newest 6.1.1, installed 5.1.7, requested ~> 5.1.7) in groups "default"
  * rails-i18n (newest 6.0.0, installed 5.1.3) in groups "default"
  * railties (newest 6.1.1, installed 5.1.7)
  * ransack (newest 2.4.2, installed 2.3.0) in groups "default"
  * shoulda-matchers (newest 4.5.1, installed 3.1.2 4b160bd) in groups "test"
  * tzinfo (newest 2.0.4, installed 1.2.9)
  * web-console (newest 4.1.0, installed 3.7.0) in groups "development"
  * webpacker (newest 5.2.1, installed 4.3.0) in groups "default"
  * websocket-driver (newest 0.7.3, installed 0.6.5)

ちなみに伊藤さんの記事で紹介されているbundle_outdated_formatterというgemを使うと下記のように表示されます(emsk/bundle_outdated_formatter: Formatter for bundle outdated command)。
結果表示までの時間もgemを使用したやりかたの方が早かったです!!
it's so beautiful!!

shell
# ローカルにインストールする
$ gem install bundle_outdated_formatter

$  docker-compose run --rm web bundle outdated | bof
Creating app_for_job_change_web_run ... done
┌──────────────────┬────────┬───────────┬─────────────────┬─────────────┐
│ gem              │ newest │ installed │ requested       │ groups      │
├──────────────────┼────────┼───────────┼─────────────────┼─────────────┤
│ actioncable      │ 6.1.1  │ 5.1.7     │                 │             │
│ actionmailer     │ 6.1.1  │ 5.1.7     │                 │             │
│ actionpack       │ 6.1.1  │ 5.1.7     │                 │             │
│ actionview       │ 6.1.1  │ 5.1.7     │                 │             │
│ activejob        │ 6.1.1  │ 5.1.7     │                 │             │
│ activemodel      │ 6.1.1  │ 5.1.7     │                 │             │
│ activerecord     │ 6.1.1  │ 5.1.7     │                 │             │
│ activesupport    │ 6.1.1  │ 5.1.7     │                 │             │
│ arel             │ 9.0.0  │ 8.0.0     │                 │             │
│ childprocess     │ 4.0.0  │ 3.0.0     │                 │             │
│ listen           │ 3.4.1  │ 3.1.5     │ >= 3.0.5, < 3.2 │ development │
│ polyamorous      │ 2.3.2  │ 2.3.0     │                 │             │
│ puma             │ 5.2.0  │ 3.12.6    │ ~> 3.7          │ default     │
│ rails            │ 6.1.1  │ 5.1.7     │ ~> 5.1.7        │ default     │
│ rails-i18n       │ 6.0.0  │ 5.1.3     │                 │ default     │
│ railties         │ 6.1.1  │ 5.1.7     │                 │             │
│ ransack          │ 2.4.2  │ 2.3.0     │                 │ default     │
│ shoulda-matchers │ 4.5.1  │ 3.1.2     │                 │ test        │
│ tzinfo           │ 2.0.4  │ 1.2.9     │                 │             │
│ web-console      │ 4.1.0  │ 3.7.0     │                 │ development │
│ webpacker        │ 5.2.1  │ 4.3.0     │                 │ default     │
│ websocket-driver │ 0.7.3  │ 0.6.5     │                 │             │
└──────────────────┴────────┴───────────┴─────────────────┴─────────────┘

めっちゃ表示されました...
アプリを作成してからバージョンアップをこまめにしてこなかったのが原因です(m_ _m)

ここで表示されている中で特に注目していただきたいのは 現状のRailsと同じバージョン番号を表示、使用しているgemバージョン番号の先頭の数字が変わっているgem です。

Railsと同じバージョン番号のgemは、Railsアプリを作成するときに必須のgemであり、Rails本体ををアップグレードすれば一緒にバージョン番号が上がると考えて問題ないと思います(actioncable, railtiesなど)。

次にバージョン番号の先頭の数字が変わっているgem(今回の場合はwebpackerregexp_parserなど)についてですが、こちらは伊藤さんが対処法を書かれていますので、一読していただければわかります。
ひどい時はアプリが動かなくなる可能性もあるみたいです。

もしgemのバージョン指定の意味がわからない場合はこちらの記事でわかると思います。

ちなみに私の場合は上記の表示されるgemの数が減りませんでした(m_ _m)
Railsのバージョンを上げていくにつれて表示される数が減っていったので、もし頑張っても数が減らないならRailsのバージョンをアップするのもありかもしれません。

手順3. developmentとtestグループのgemをバージョンアップする

ここで本番環境に影響の出ないdevelopment環境とtest環境のgemをバージョンアップします。
その前にGemfileにとても大事な変更を行います。
それはgemのバージョン指定の記述を削除することです(Railsは対象外)。
下記のように全てのgemに対してバージョン指定を削除しましょうというお話です。

Gemfile
- gem 'mysql2', '>= 0.3.18', '< 0.6.0'
- gem 'puma', '~> 3.7'
+ gem 'mysql2'
+ gem 'puma'
gem 'rails', '~> 5.1.7' # Railsのgemのバージョン指定は削除しないこと!!

Rails以外のバージョンアップするにはshellで下記のコマンドを実行します。
developmentとtest環境からアップグレードします。

shell
$ docker-compose run --rm web bundle update -g development -g test
shell
$ docker-compose run --rm web bundle update -g development -g test                                                                                                
Creating app_for_job_change_web_run ... done
Fetching https://github.com/thoughtbot/shoulda-matchers.git
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies.........
:
# gemのインストールなどは省略します
:
:
Bundler attempted to update awesome_print but its version stayed the same
Bundler attempted to update byebug but its version stayed the same
Bundler attempted to update factory_bot_rails but its version stayed the same
Bundler attempted to update faker but its version stayed the same
Bundler attempted to update gimei but its version stayed the same
Bundler attempted to update pry-byebug but its version stayed the same
Bundler attempted to update pry-rails but its version stayed the same
Bundler attempted to update rails-flog but its version stayed the same
Bundler attempted to update rspec-rails but its version stayed the same
Bundler attempted to update capybara but its version stayed the same
Bundler attempted to update launchy but its version stayed the same
Bundler attempted to update rspec_junit_formatter but its version stayed the same
Bundler attempted to update selenium-webdriver but its version stayed the same
Bundler attempted to update shoulda-matchers but its version stayed the same
Bundler attempted to update simplecov but its version stayed the same
Bundler attempted to update vcr but its version stayed the same
Bundler attempted to update webdrivers but its version stayed the same
Bundler attempted to update webmock but its version stayed the same

同じようなWARNINGがたくさん表示されました。

Bundler attempted to update xxx but its version stayed the same
をそのままググるとリンクのようにこの警告が原因で、Rails自体がアップグレードできない場合もあるみたいです。

私の場合は色々試みましたがこの時点では解決できなかったので、放置して次の手順へ進みました。
一応念のため、RSpecでテストを実行して動作的に問題がないことを確認してから次に進めています。
(ちなみにアプリのアップグレードが完了した後もdocker-compose run --rm web bundle update -g development -g testを実行すると同じようにログが出ます... 解消できない...)

手順4. Rails以外ののgemを全てバージョンアップする

今度は本番環境を含むgemをバージョンアップしましょう
ここもRailsは対象外です
やっていることは上とほぼ同じなので割愛しますが

  1. docker-compose run --rm web bundle updateを実行する
  2. docker-compose run --rm web rspecでテストを実行する

という手順をふみます。

問題なくバージョンアップできていれば次に進みます。
例のごとくRSpecでテストを実行して動作的に問題がないことを確認します。

さぁ、
これで準備できたから
Railsをアップグレードするぞ!!

とはなりません。

手順5. Rubyを(できるだけ)バージョンアップする

※Rubyのバージョンが最新安定版であれば問題ありません。次のRailsをアップグレードする手順へ進んでください。

Rubyを段階的にアップグレードします(2.6.6 -> 2.7.2みたいな感じ)。
ちなみに2021年1月現在の最新安定版のRubyバージョンは 3.0.0 です(Rubyのトップページ)。

理由としては下記が挙げられます。

  • Rubyをアップグレードすると仕様が変わることがある(アプリが動かなくなる原因にも)
  • セキュリティやパフォーマンスに悪い影響が出る可能性がある

なのでアップグレードは定期的にしましょう!!

そして個人的に重要だと思うのはアップグレードすることに対して十分な時間と心のゆとりをもつことです。
それさえあればなんとかなります(なると思います)。

私の場合は記事投稿地点でのRubyのバージョンが2.5.7です。
最新のRubyバージョンまでアップするためには

2.5.7 ~> 2.6.6 ~> 2.7.2 ~> 3.0.0

32回のアップグレードが必要になります。

では実際にアップグレードしていきます。

まずDockerfileの記述を変更します。

Dockerfile
- ARG RUBY_VERSION=2.5.7
+ ARG RUBY_VERSION=2.6.6
FROM ruby:$RUBY_VERSION

ちなみにここで指定しているイメージの数値は必ずDockerHubのRuby公式イメージに存在する数値に書き換えてください!!(ruby - Docker Hub)
私は2.6.6にすべき値を2.6.0とDockerHubを確認せずに変更して、エラーの原因がそれだと気づかず数日を浪費しました。

話を戻しますが、
私はDockerfileで定義命令としてARGを使っているので上記の書き方になりますが、使っていなければ

Dockerfile
- FROM ruby:2.5.7
+ FROM ruby:2.6.6

という書き方になります。

その後下記コマンドを順に実行していきます。

shell
docker-compose build --no-cache
docker-compose run --rm web bundle install
# mv Gemfile.lock tmp <- bundle install がうまくいかないときに実行
# docker-compose run --rm web bundle install <- mv Gemfile.lock tmp を実行したら再度bundle installする
docker-compose run --rm web gem pristine --all # これを実行すると Ignoring <gem_name>-<version> because its extensions are not built. Try: gem pristine <gem_name> --version <version> という警告が表示されなくなる

上記のコマンドの意味は下記に書いておきます。

  • docker-compose build --no-cache: cacheを使わずにイメージをビルドする。cacheが有効になっていると設定変更がうまくされない場合があります(docker-compose build | Docker ドキュメント)。今回はかなり大きめな変更なので--no-cacheオプションを付加します。
  • mv Gemfile.lock tmp: Gemfile.lockが原因でうまくgemが入らない時があります。そんなときはGemfile.lockの中の記述を空にする方法がありますが、それと同じ効果があります。
  • docker-compose run --rm web bundle install: Rubyのアップグレードを行ったのでgemの対応バージョンも変化する場合があるので実行します。
  • docker-compose run --rm web gem pristine --all:詳細はこのリンクに委ねます。

そしてここでRSpecでテストします。

問題なければ、DockerのRubyイメージがちゃんと変更した新しいバージョンかチェックしましょう。
チェックするには下記のコマンドを実行します。

shell
$  docker-compose run --rm web ruby -v
Creating app_for_job_change_web_run ... done
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]

Dockerfileで変更した数値と合致していれば成功です。
最後に隠しファイルである.ruby-versionを開いてみてください。
私の場合は後から気づいたのですが、基本的には自動でRailsアプリで使用しているRubyのバージョンに自動で切り替わるらしいのですが、私の場合は切り替わらなかったので自分で修正しました。

.ruby-version
- 2.5.7
+ 2.6.6

これでRubyのアップグレードは(とりあえず)完了です
そしてやっと本題のRailsのアップグレードに取り掛かります。

ちなみに

  • Rubyのイメージのバージョンを変更して(docker-compose build --no-cache)、bundle installも問題なくできた場合、それ以降で発生するエラーは通常のRailsで発生するエラーと同じはずです。 じっくり、こつこつと修正していきましょう^ ^
  • 試していてわかったのですが、Rubyの3.0.0とRailsの5.2.4.4は現在互換性がありません(調べ方はこのリンク過去も含めた複数バージョンへのサポートを提供しているgemというところに書いてある)。
  • Rubyの3.0.0と互換性のあるRailsのバージョンは6.1.0以上のようです(アプリケーションをRuby3にあげるときにやること - Qiita)。
  • この記事ではとりあげていませんが、CircleCIを使用されている場合はCircleCI側のRubyのバージョンも変更する必要があるので注意してください。

自分の事例をそのままま書くと下記になります。

アップグレード Ruby ver Rails ver 可否
初期状態 2.5.7 5.1.7 OK
1回目 2.6.6 5.1.7 OK
2回目 2.7.2 5.1.7 OK
3回目 2.7.2 5.2.4.4 OK
4回目 2.7.2 6.0.3.4 OK
5回目 3.0.0 6.0.3.4 NG
4回目 2.7.2(ダウングレード) 6.0.3.4 OK

手順6. Railsをバージョンアップする

ようやく本題、Railsのアップグレードにとりかかります

ここで注意しなければならないことは、当たり前かもしれませんが
RubyとRailsの互換性を確認すること です。
これが合っていないと作成したアプリが壊れてしまいかねませんのでご注意ください。
確認方法はRails アップグレードガイド - Railsガイドに互換性のあるRubyとRailsのバージョンが記載されています。
ちなみに下記リンクはRailsの全バージョン履歴です(Rubyの全バージョン履歴わかれば誰か教えてください)。

railsの全バージョン履歴 | RubyGems.org | コミュニティのGemホスティングサービス

私の作成当初の構成は

  • Ruby: 2.5.7
  • Rails: 5.1.7

だったので
Railsはまず5.2.4.4にまで上げることにしました。

Railsのバージョンを上げるためには Gemfile の記述を下記のように変更します。

Gemfile
- gem 'rails', '~> 5.1.7'
+ gem 'rails', '5.2.4.4'

バージョンは最新のパッチバージョンでカッチリ固定してあげてください。
準備ができたらshellで下記コマンドを実行してRailsも含めたバージョンアップを行います

shell
docker-compose run --rm web bundle update

バージョンアップが完了したらRspecでテストをします。
テストがOKならば無事Railsのアップグレードがひとまず完了です。

手順7. load_defaultsやnew_framework_defaults_x_x.rbを設定する

伊藤さんの記事へのリンクをそのまま貼らせていただいてますが、
アップグレードによってデフォルトの挙動が変わったりする時があります。
それを前のバージョンのと同じように動作させるための設定をする必要がある場合もあります。

config.load_defaultsとnew_framework_defaults_x_x.rbの関係を詳しく調べてみた - Qiita

Ruby、Railsのアップグレードの流れは完了

これでDocker + Ruby + Rails アップグレードの流れは理解できたと思います。
バージョンが古い場合は大変な労力と時間がかかりますが、手順の5から7を繰り返せばアップグレードはできると思います!!

警告一覧

HEADS UP! i18n 1.1 changed fallbacks to exclude default locale. But that may break your application.

docker-compose run --rm web bundle updateをした時にgem Deviseから表示されました。
ログとしては下記のように表示されました。

terminal
HEADS UP! i18n 1.1 changed fallbacks to exclude default locale.
But that may break your application.

Please check your Rails app for 'config.i18n.fallbacks = true'.
If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be
'config.i18n.fallbacks = [I18n.default_locale]'.
If not, fallbacks will be broken in your app by I18n 1.1.x.

For more info see:
https://github.com/svenfuchs/i18n/releases/tag/v1.1.0

If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be 'config.i18n.fallbacks = [I18n.default_locale]'.でググると出てくる、下記の4つのリンクを読めば解決方法や詳細がわかります。

Ignoring bigdecimal-1.3.5 because its extensions are not built. Try: gem pristine bigdecimal --version 1.3.5

docker-compose build --no-cacheを実行してbundle installを実行したときに表示されました。
docker-compose run --rm web gem pristine --allを実行すれば表示されなくなります。

The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java.

bundle installbundle update時に表示される。
基本的にWindows以外の開発環境では必要ないと思われるので、
削除する or コメントアウトする という対処法で問題ないと思われます。
下記リンクに詳細が書いてあります。

Bundler attempted to update rails but its version stayed the same

Rubyが2.7.2、Railsが6.0.3.4の時にbundle update railsを実行しようとして表示された警告です。。
使っていたBundlerのバージョンが1.17.3と古く、バージョンアップしてくださいという警告です。
[Ruby] bundler 2 へのアップグレード方法 | DevelopersIOを参考に進めていきます。

terminal
$ docker-compose run --rm web bundle update --bundler
Creating app_for_job_change_web_run ... done
Using rake 13.0.3
Using concurrent-ruby 1.1.8
Using i18n 1.8.8
Using minitest 5.14.3
:
:
:
Warning: the lockfile is being updated to Bundler 2, after which you will be unable to return to Bundler 1.

上記のコマンドを使用して私の場合はBundlerのバージョンが 1.17.4 -> 2.1.4 に無事バージョンアップができました。

DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :name attribute in Foodcategory model, passcase_sensitive: trueoption explicitly to the uniqueness validator.

Railsを 5.2系 から 6.0系 へバージョンアップしたときに表示されます。
解決法(?)は下記がわかりやすいです

Rails 6.0で"Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1."という警告が出たときの対処法 - Qiita

エラー一覧

Your Ruby version is x.x.x, but your Gemfile specified y.y.y

GemfileにRubyのバージョンを明記している場合にエラーが発生します。
指定を修正してあげるだけでエラーが解消できます。

Gemfile
- ruby 'y.y.y'
+ ruby 'x.x.x
warning: already initialized constant Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher::ARBITRARY_OUTSIDE_STRING

terminal上では下記のように表示されていました。
(Railsは5.1系のまま、)Rubyを2.6.6から2.7.2に上げた時にRSpecのテストで発生したエラーです。

terminal
Failure/Error: require File.expand_path('../config/environment', __dir__)

NoMethodError:
  undefined method `new' for BigDecimal:Class
# /usr/local/bundle/bundler/gems/shoulda-matchers-4b160bd19ecc/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb:273:in `<class:ValidateInclusionOfMatcher>'

私の場合はtest環境に追加されているgemのshoulda-matchersのオプション(?)を削除することで問題なく動作するようになりました(rails5系でもオプションを指定する必要がいつのまにかなくなっていました)。

Gemfile
- gem 'shoulda-matchers',
-     git: 'https://github.com/thoughtbot/shoulda-matchers.git',
-     branch: 'rails-5'
+ gem 'shoulda-matchers',
undefined method `operations' for #<ActionDispatch::MiddlewareStack

wrong number of arguments (given 3, expected 2) というパターンもありました。
(Railsは5.1系のまま)Rubyを2.7.2から3.0.0に上げた時にRSpecのテストで発生したエラーです。
原因はRubyとRailsの互換性にありました。
Ruby3.0.0に対応しているRailsのバージョンは6.1.0以上のようです。
Rubyを2.7.2に戻せばエラーは解消されます。

terminal上でのログは下記の通りです。

terminal
  docker-compose run --rm web rspec
Creating app_for_job_change_web_run ... done
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:29: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:118: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:118: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:35: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:35: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:44: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:118: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.
/usr/local/bundle/gems/bundler-1.17.3/lib/bundler/shared_helpers.rb:35: warning: Pathname#untaint is deprecated and will be removed in Ruby 3.2.

An error occurred while loading ./spec/features/foodcategory_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)

ArgumentError:
  wrong number of arguments (given 3, expected 2)
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/static.rb:109:in `initialize'
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/stack.rb:35:in `new'
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/stack.rb:35:in `build'
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/stack.rb:99:in `block in build'
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/stack.rb:99:in `each'
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/stack.rb:99:in `inject'
# /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/middleware/stack.rb:99:in `build'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:508:in `block in app'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:504:in `synchronize'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:504:in `app'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/application/finisher.rb:45:in `block in <module:Finisher>'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:30:in `instance_exec'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:30:in `run'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:59:in `block in run_initializers'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:58:in `run_initializers'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# ./spec/features/foodcategory_spec.rb:1:in `<top (required)>'

An error occurred while loading ./spec/models/cuisine_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)

NoMethodError:
  undefined method `operations' for #<ActionDispatch::MiddlewareStack:0x000055c6d7c7e388 @middlewares=[Rack::Sendfile, ActionDispatch::Static, ActionDispatch::Executor, ActiveSupport::Cache::Strategy::LocalCache::Middleware, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, ActionDispatch::RemoteIp, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::Callbacks, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, Rack::Head, Rack::ConditionalGet, Rack::ETag, Warden::Manager]>
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/configuration.rb:76:in `+'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/application.rb:525:in `build_middleware'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:507:in `block in app'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:504:in `synchronize'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:504:in `app'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/application/finisher.rb:45:in `block in <module:Finisher>'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:30:in `instance_exec'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:30:in `run'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:59:in `block in run_initializers'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/initializable.rb:58:in `run_initializers'
# /usr/local/bundle/gems/railties-5.1.7/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `require'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `block in require'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:258:in `load_dependency'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `require'
# ./spec/rails_helper.rb:3:in `<top (required)>'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `require'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `block in require'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:258:in `load_dependency'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:292:in `require'
# ./spec/models/cuisine_spec.rb:1:in `<top (required)>'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:286:in `load'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:286:in `block in load'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:258:in `load_dependency'
# /usr/local/bundle/gems/activesupport-5.1.7/lib/active_support/dependencies.rb:286:in `load'
/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)

Ruby2.7.2の状態でRailsを5.2.4.4にアップグレードしたあとの動作確認中に発生したエラー。
Rails5.2からbootsnapが導入されましたが、Gemfileに自動的にbootsnapが追記されず、自分で追記していなかったことが原因です。
Gemfileに下記のように追記することでエラーが解消しました。
導入されたことがわかるのはRailsDiffが、
bootsnapについてはbootsnapのせいでRails5.2とかが動かない人へ - Qiita
がわかりやすいです。

Gemfile
gem 'bootsnap', '>= 1.1.0', require: false
Can't resolve image into URL: undefined method `to_model'

Ruby2.7.2の状態でRailsを5.2.4.4にアップグレードしたあとの動作確認中に発生したエラー。
画像アップロード用のgemとしてcarrierwaveを使用しており、それに伴うエラーです。

そのままググると全く同じ状況のエラー解決法があったので(ruby on rails - Carrierwave: Can't resolve image into URL: undefined method `to_model' - Stack Overflow)参考にしたところ解決しました。

ちなみにActiveStrageでも同じようなことが起こる場合があるみたいです(【Rails】ActiveStorageを用いた画像複数枚投稿のエラー - Qiita)

Migrations are pending. To resolve this issue, run: rails db:migrate RAILS_ENV=development

Ruby2.7.2のときに
Railsを 5.2.4.4から 6.0.3.4 にアップグレードした際に発生したエラーです。
このアップグレードにはどうやら標準で搭載されているActiveStrageに対してのDB設計の修正が行われており、
アップグレードした際にマイグレーションファイルが追加されています。
追加されたmigrationファイルをDBに反映させてあげればいいだけなので下記コマンドを実行することでエラーが解消されます。

terminal
docker-compose run --rm web rails db:migrate

最後に

Qiita初投稿のこの記事を書くのに何日費やしたのかわかりません(カウントしておけばよかった...)。
普段から引用して問題解決などしていますが、良記事を投稿している方の偉大さがとても身にしみました。

参考リンクなどなど

27
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
13