Rails 6 アップグレードで Mongoid が読み込まれないという懸念がある
概要
私たちはアプリを Ruby 2.7 から 3.0 に、Rails 5.2 から 6.0 に同時にアップグレードしています (私は Ruby だけから始めましたが、3.0 は 5.2 と互換性がないようです)。近いですが、ユーザー モデルのフィールドを懸念に分割すると、起動時に奇妙なエラー: uninitialized constant UserFields::Boolean (NameError) が発生します。
class User
include Mongoid::Document
include Mongoid::Timestamps
#field :force_password_change, type: Boolean, default: false
include UserFields
....
end
module UserFields
extend ActiveSupport::Concern
included do
## Database authenticatable
field :email, :type => String, :default => ""
field :force_password_change, type: Boolean, default: false ### <<<< ERROR HERE
end
end
ブール値フィールドをモデルに直接配置すると、正常に機能しますが、それらが懸念される場合は機能しません。エラートレース:
! Unable to load application: NameError: uninitialized constant UserFields::Boolean
2024-01-10 15:22:22 bundler: failed to load command: puma (/app/bundle_cache/ruby/3.0.0/bin/puma)
2024-01-10 15:22:22 /app/app/models/concerns/user_fields.rb:8:in `block in <module:UserFields>': uninitialized constant UserFields::Boolean (NameError)
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/concern.rb:122:in `class_eval'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/concern.rb:122:in `append_features'
2024-01-10 15:22:22 from /app/app/models/user.rb:9:in `include'
2024-01-10 15:22:22 from /app/app/models/user.rb:9:in `<class:User>'
2024-01-10 15:22:22 from /app/app/models/user.rb:4:in `<main>'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/bootsnap-1.17.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:30:in `require'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/inflector/methods.rb:282:in `const_get'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/inflector/methods.rb:282:in `block in constantize'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/inflector/methods.rb:280:in `each'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/inflector/methods.rb:280:in `inject'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/inflector/methods.rb:280:in `constantize'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/activesupport-6.0.6.1/lib/active_support/dependencies/zeitwerk_integration.rb:19:in `constantize'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise.rb:325:in `get'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/mapping.rb:83:in `to'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/mapping.rb:78:in `modules'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/mapping.rb:95:in `routes'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/mapping.rb:162:in `default_used_route'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/mapping.rb:72:in `initialize'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise.rb:361:in `new'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise.rb:361:in `add_mapping'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/rails/routes.rb:243:in `block in devise_for'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/rails/routes.rb:242:in `each'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/devise-4.9.3/lib/devise/rails/routes.rb:242:in `devise_for'
2024-01-10 15:22:22 from /app/config/routes.rb:264:in `block in <main>'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/actionpack-6.0.6.1/lib/action_dispatch/routing/route_set.rb:426:in `instance_exec'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/actionpack-6.0.6.1/lib/action_dispatch/routing/route_set.rb:426:in `eval_block'
2024-01-10 15:22:22 from /app/bundle_cache/ruby/3.0.0/gems/actionpack-6.0.6.1/lib/action_dispatch/routing/route_set.rb:408:in `draw'
2024-01-10 15:22:22 from /app/config/routes.rb:1:in `<main>'
Devise を使用し、User モデルを使用しているため、これはルート ファイルに由来します。同じコードが 5.2/2.7 で何年も機能していたので、これはアップグレードに関係していると確信しています。助けていただければ幸いです。
解決策
更新: これは、mongoid gem のアップグレードに関係している可能性があります。以前に使用していたバージョンはわかりませんが、7.3 未満ではトップレベルの定数 ::Boolean が定義されていたようです。これにより、Boolean Source を作成しただけであれば適切に解決できるようになります。新しいバージョンには、この最上位の定数は含まれていません。
リリースノートには次のように記載されています。
懸念事項は Mongoid::Document を含むクラスのコンテキストの外側で評価されるため、定数を Mongoid::Boolean として明示的に修飾する必要があります。
例えば
module UserFields
extend ActiveSupport::Concern
included do
## Database authenticatable
field :email, type: String, default: ""
field :force_password_change, type: Mongoid::Boolean, default: false
end
end