世界が幸せで在ります様に

ITエンジニアになりたい人・エンジニアの人にとって役立ちそうな商品を紹介するブログ

【初心者向け】Railsのアップデートのやり方を簡単に習得しよう(Rails 5.2.2へのアップデート)

rails5.2.2がリリースされました。

今回は、初心者向けにrailsのアップデートのやり方を簡単に記事にしてみました。

実際に、アップデートは誰もが通る道なのでその手順をまとめてみましたので、時間を見つけて是非アップデートしてみましょう。本当に簡単にできてしまいます。

公式を見る

rubyonrails.org

確かに5.2.2がLatest versionであることを確認し、変更点なども同時に見ておきましょう。

今回は、バグフィックスバージョンなので後方互換性は基本影響ありませんが、モンキーパッチなどをしている場合はちゃんと読むのが良いと思われます。

環境

  • ruby 2.5.3
  • rails 5.2.1
    • 適当に作ったプロジェクト

手順

Gemfileを変更する

gem 'rails', '~> 5.2.2'

コマンドを打つ

bundle install

メッセージ

Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "actionview":
  In snapshot (Gemfile.lock):
    actionview (= 5.2.1)

  In Gemfile:
    haml-rails was resolved to 1.0.0, which depends on
      actionpack (>= 4.0.1) was resolved to 5.2.1, which depends on
        actionview (= 5.2.1)

    rails (~> 5.2.2) was resolved to 5.2.2, which depends on
      actionview (= 5.2.2)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

これは、bundle updateをしてほしいというメッセージになります。ただし、余計なメッセージは最初は気にせずにIn snapshotのところだけを見ましょう。

bundle updateとはなんなのかについて軽く書いておきます

bundle installというのはみなさんよく使うと思います。これは、GemfileGemfile.lockの差分のgemをインストールしてGemfile.lockを更新するというものになります。

bundle updateは、installとは違い差分を見るのではなく、Gemfileのみを見てインストールします。そして、Gemfile.lockを更新します。

すなわち、先程のメッセージで言っていることは、Gemfile.lockに更新をかけにいったけど、なんか依存関係がおかしいよ??ということです。

具体的には、railsのバージョンが5.2.1から5.2.2に変わっているので、rails5.2.1を見ているgemあたりでエラーが出るはずです。

では一つづつ変更をしていくのが良いでしょう。

actionviewをupdateに追加

In snapshotにあるactionviewで失敗しているのでそれを加えてあげましょう。

bundle update --conservative rails actionview
メッセージ
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "activemodel":
  In snapshot (Gemfile.lock):
    activemodel (= 5.2.1)

  In Gemfile:
    activerecord-import was resolved to 0.27.0, which depends on
      activerecord (>= 3.2) was resolved to 5.2.1, which depends on
        activemodel (= 5.2.1)

    rails (~> 5.2.2) was resolved to 5.2.2, which depends on
      activemodel (= 5.2.2)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

activemodelをupdateに追加

同様にメッセージで言及されているactivemodelを追加します。余計なものは加えなくて良いです。まずはIn snapshotで記述された依存関係の失敗を機械的に加えていきましょう

bundle update --conservative rails actionview activemodel
メッセージ
Bundler could not find compatible versions for gem "activesupport":
  In snapshot (Gemfile.lock):
    activesupport (= 5.2.1)

  In Gemfile:
    rails (~> 5.2.2) was resolved to 5.2.2, which depends on
      activesupport (= 5.2.2)

    rails-i18n was resolved to 5.1.2, which depends on
      railties (< 6, >= 5.0) was resolved to 5.2.1, which depends on
        activesupport (= 5.2.1)

activesupportをupdateに追加

メッセージで言及されているactivesupportを追加します。

bundle update --conservative rails actionview activemodel activesupport
メッセージ
Bundler could not find compatible versions for gem "actionpack":
  In snapshot (Gemfile.lock):
    actionpack (= 5.2.1)

  In Gemfile:
    rails (~> 5.2.2) was resolved to 5.2.2, which depends on
      actionpack (= 5.2.2)

    coffee-rails (~> 4.2) was resolved to 4.2.2, which depends on
      railties (>= 4.0.0) was resolved to 5.2.1, which depends on
        actionpack (= 5.2.1)

actionpackをupdateに追加

メッセージで言及されているactionpackを追加します。

bundle update --conservative rails actionview activemodel activesupport actionpack
メッセージ
Bundler could not find compatible versions for gem "railties":
  In snapshot (Gemfile.lock):
    railties (= 5.2.1)

  In Gemfile:
    coffee-rails (~> 4.2) was resolved to 4.2.2, which depends on
      railties (>= 4.0.0)

    devise was resolved to 4.5.0, which depends on
      railties (< 6.0, >= 4.1.0)

    haml-rails was resolved to 1.0.0, which depends on
      railties (>= 4.0.1)

    jquery-rails was resolved to 4.3.3, which depends on
      railties (>= 4.2.0)

    rails (~> 5.2.2) was resolved to 5.2.2, which depends on
      railties (= 5.2.2)

    rails-i18n was resolved to 5.1.2, which depends on
      railties (< 6, >= 5.0)

    devise was resolved to 4.5.0, which depends on
      responders was resolved to 2.4.0, which depends on
        railties (< 5.3, >= 4.2.0)

    sass-rails (~> 5.0) was resolved to 5.0.7, which depends on
      railties (< 6, >= 4.0.0)

    web-console (>= 3.3.0) was resolved to 3.7.0, which depends on
      railties (>= 5.0)

railtiesをupdateに追加

メッセージが長いので初めて見ると圧倒されてしまうかもしれませんが、要はIn snapshotだけ見れば良いのでrailtiesというrailsのコアgemだけを加えればよいのです。

bundle update --conservative rails actionview activemodel activesupport actionpack railties
メッセージ
Bundler could not find compatible versions for gem "activerecord":
  In snapshot (Gemfile.lock):
    activerecord (= 5.2.1)

  In Gemfile:
    activerecord-import was resolved to 0.27.0, which depends on
      activerecord (>= 3.2)

    rails (~> 5.2.2) was resolved to 5.2.2, which depends on
      activerecord (= 5.2.2)

activerecordをupdateに追加

メッセージで言及されているactiverecordを追加します。

bundle update --conservative rails actionview activemodel activesupport actionpack railties activerecord
# 省略
Bundle updated!

無事にbundle update成功しました。思ったよりサクッとできたのではないでしょうか?

試しに、bundle installをしてみましょう。問題がなければそのままインストールが成功します。

あとがき

rails 5.2.2は、バグフィックスバージョンなので、自分の環境では影響は出ませんでした。bundle update後にconfigファイルを変更するといった作業は必要ないため、アップデートのやり方は思ったより簡単に見えるかもしれません。

しかし、これがメジャーバージョンの変更になってくると、bundle updateをしたあとに、configファイルの変更も必要になってくるケースがあるので注意しましょう。

基本的な内容にはなりますが、以下の技術書を読んでおけば問題ないと思われます。

現場で使える Ruby on Rails 5速習実践ガイド

現場で使える Ruby on Rails 5速習実践ガイド

  • 作者: 大場寧子,松本拓也,櫻井達生,小田井優,大塚隆弘,依光奏江,銭神裕宜,小芝美由紀
  • 出版社/メーカー: マイナビ出版
  • 発売日: 2018/10/19
  • メディア: 単行本(ソフトカバー)
  • この商品を含むブログを見る

Ruby on Rails 5アプリケーションプログラミング

Ruby on Rails 5アプリケーションプログラミング

プロフィールと免責事項