代码之家  ›  专栏  ›  技术社区  ›  timsegraves

Rails 6升级未从一个问题加载Mongoid

  •  0
  • timsegraves  · 技术社区  · 2 年前

    我们正在将我们的应用程序从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中已经使用了多年。任何帮助都将不胜感激。

    1 回复  |  直到 2 年前
        1
  •  1
  •   engineersmnky    2 年前

    这可能与 zietwerk (这当然是一个不断查找的问题),但解决方案应该像更具体一样简单。

    你应该能够改变 Boolean 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  ### <<<< ERROR HERE
      end
    end
    
    推荐文章