LoginSignup
4
2

Ruby 3.0.1 -> 3.2.2、Rails 6.1.7 -> 7.0.8 へのアップデート時のエラーと対応

Last updated at Posted at 2023-09-14

はじめに

タイトルの通り今回 Ruby 3.0.1 -> 3.2.2、Rails 6.1.7 -> Rails 7.0.8 へのアップデートを行った中で出たエラーに対応した内容を記した記事になります。
ruby や rails の新機能や設定等は触れず、単にそのまま bundle update した時に出るエラーの解消を目的としています。
すでに様々な記事で似たような対応されているところがあると思いますが、何かのお役に立てば幸いです。

uninitialized class variable @@schemes in URI (NameError) Did you mean? scheme_list

/vendor/bundle/ruby/3.2.0/gems/data_uri-0.1.0/lib/data_uri/uri.rb:64:in `<module:URI>': uninitialized class variable @@schemes in URI (NameError)
Did you mean?  scheme_list

※2023/11/13更新
以前記載していた方法ではダメだったのでこの項は丸々変更しました。

carrierwave-data-uri が依存している data_uriURI::Generic クラスでクラス変数が使用不可になった仕様変更に対応していないため。
require 'data_uri' した段階でエラーになってしまうため、URI gem のバージョンを下げて対応。

Gemfile
gem 'uri', '~> 0.10.3'

Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`. (Psych::AliasesNotEnabled)

YAML.load 時に yml ファイルで & のエイリアスを指定していたため aliases オプションを追加

YAML.load(ERB.new(IO.read('config/settings.yml')).result, aliases: true)

Missing template layouts/mgre/api/v1/application.json.jb with {:locale=>[:ja], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :jb, :slim]}. (ActionView::MissingTemplate)

ApplicationControllerlayout の指定は拡張子不要のため削除

application_controller.rb
layout 'api/v1/application'

Error: The `RSpec/FilePath` cop has been split into `RSpec/SpecFilePathFormat` and `RSpec/SpecFilePathSuffix`.

rubocop-rspecv2.24.0 の変更により RSpec/FilePathRSpec/SpecFilePathFormatRSpec/SpecFilePathSuffix に分割された模様。
rails の inflection に定義を追加しているので CustomTransform を使用していたため、以下のように変更。

旧.rubocop.yml
RSpec/FilePath:
  CustomTransform:
    hoge: HOGE
新.rubocop.yml
RSpec/SpecFilePathFormat:
  CustomTransform:
    hoge: HOGE

ちなみに 2.24.0 の Changelog には RSpec/FilePath cop is enabled by default と書いてあったけど普通にエラーになりました。

ActionView::MissingTemplate: Missing template xxx/yyy/zzz with {:locale=>[:ja], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :jb, :slim]}.

7 にして MissingTemplate が出た場合はビューファイルのファイル名を show.html.erb -> show.erb のように拡張子を1つのみに変更することで正しく見つけられるようになる場合がある。
他には formats: :html のようにオプションを指定することで修正できたという issue を見かけたものの、自分の環境では修正確認できず。

`connected_to': unknown keyword: :database (ArgumentError)

rails-on-services/apartment を使っている場合に起こるエラー。この PR で修正されているがまだリリースされていない(最新 v2.11.0 時点)ためパッチを当てて対応。


ActiveRecord::SubclassNotFound

STI クラスを使っているモデルで以下のエラー。

The single-table inheritance mechanism failed to locate the subclass: 'カラム名'. This error is raised because the column 'カラム名' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite クラス名.inheritance_column to use another column for that information. (ActiveRecord::SubclassNotFound)

STI に指定したカラムでサブクラスが見つからないという内容。該当部分では enumerize と組み合わせて以下のように列挙型の STI クラスを使っていた。

enumerize :type, in: {
  type1: 'MyApp::MyModule::Type1',
  type2: 'MyApp::MyModule::Type2',
}, default: :once_only, scope: true

これがこちらの PRで、ActiveSupport::Dependencies.constantize が削除され String#constantize に移行された模様。

activerecord-6.1.7.3/lib/active_record/inheritance.rb
# rails 6.1.7.3
def sti_class_for(type_name)
  if store_full_sti_class && store_full_class_name
    ActiveSupport::Dependencies.constantize(type_name)
  else
    compute_type(type_name)
  end
  ...
activerecord-7.0.8/lib/active_record/inheritance.rb
# rails 7.0.8
def sti_class_for(type_name)
  if store_full_sti_class && store_full_class_name
    type_name.constantize
  else
    compute_type(type_name)
  end
  ...

以下のようにパッチで対応。

config/initializers/active_record/inheritance.rb
require 'active_record/inheritance'

module MyActiveRecord
  module Inheritance
    module ClassMethods
      # enumerize のインスタンスであれば value をクラス名に使う
      def sti_class_for(type_name)
        type_name = type_name.value if type_name.is_a? Enumerize::Value
        super type_name
      end
    end
  end
end

module ActiveRecord
  module Inheritance
    module ClassMethods
      prepend MyActiveRecord::Inheritance::ClassMethods
    end
  end
end


今後修正箇所増えるたびに追記していきます。

4
2
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
4
2