0

尝试将现有的 rails 应用程序从 ruby​​ 2.7.2 升级到 ruby​​ 3.0。

尝试使用 启动服务器时出错rails s。以下是错误的完整日志:

rails s
=> Booting Puma
=> Rails 6.1.3.1 application starting in development 
=> Run `bin/rails server --help` for more startup options
Called 'load' without the :safe option -- defaulting to safe mode.
You can avoid this warning in the future by setting the SafeYAML::OPTIONS[:default_mode] option (to :safe or :unsafe).
Exiting
/Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activerecord-6.1.3.1/lib/active_record/associations/builder/association.rb:53:in `build_scope': undefined method `arity' for {:class_name=>"Role", :as=>:resource, :dependent=>:destroy}:Hash (NoMethodError)
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activerecord-6.1.3.1/lib/active_record/associations/builder/association.rb:47:in `create_reflection'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activerecord-6.1.3.1/lib/active_record/associations/builder/association.rb:32:in `build'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activerecord-6.1.3.1/lib/active_record/associations.rb:1458:in `has_many'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rolify-5.1.0/lib/rolify.rb:51:in `resourcify'
    from /Users/mj068550/Documents/Git/OPSIMPSAO-21190/ClosedStack/app/models/client.rb:5:in `<class:Client>'
    from /Users/mj068550/Documents/Git/OPSIMPSAO-21190/ClosedStack/app/models/client.rb:4:in `<top (required)>'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:523:in `block in load_file'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:715:in `new_constants_in'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:522:in `load_file'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:419:in `block in require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:39:in `block in load_interlock'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:39:in `load_interlock'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:402:in `require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:49:in `block in require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:17:in `allow_bootsnap_retry'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:558:in `load_missing_constant'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:61:in `block in load_missing_constant'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:17:in `allow_bootsnap_retry'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:60:in `load_missing_constant'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:213:in `const_missing'
    from /Users/mj068550/Documents/Git/OPSIMPSAO-21190/ClosedStack/app/datatables/client/remedy_domains_datatable.rb:3:in `<top (required)>'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:523:in `block in load_file'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:715:in `new_constants_in'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:522:in `load_file'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:419:in `block in require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:39:in `block in load_interlock'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:39:in `load_interlock'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:402:in `require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:49:in `block in require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:17:in `allow_bootsnap_retry'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:48:in `require_or_load'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:375:in `depend_on'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/active_support.rb:86:in `depend_on'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:288:in `require_dependency'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/engine.rb:493:in `block (2 levels) in eager_load!'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/engine.rb:492:in `each'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/engine.rb:492:in `block in eager_load!'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/engine.rb:489:in `each'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/engine.rb:489:in `eager_load!'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/coverband-4.2.7/lib/coverband/utils/railtie.rb:7:in `eager_load!'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/application.rb:512:in `eager_load!'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/engine.rb:358:in `eager_load!'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/application/finisher.rb:134:in `each'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/application/finisher.rb:134:in `block in <module:Finisher>'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/initializable.rb:32:in `instance_exec'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/initializable.rb:32:in `run'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/initializable.rb:61:in `block in run_initializers'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:347:in `each'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:347:in `call'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
    from /Users/mj068550/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/initializable.rb:60:in `run_initializers'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/application.rb:384:in `initialize!'
    from /Users/mj068550/Documents/Git/OPSIMPSAO-21190/ClosedStack/config/environment.rb:5:in `<top (required)>'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `block in require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:299:in `load_dependency'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `require'
    from config.ru:5:in `block in <main>'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/builder.rb:116:in `eval'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/builder.rb:116:in `new_from_string'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/builder.rb:105:in `load_file'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/builder.rb:66:in `parse_file'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/server.rb:349:in `build_app_and_options_from_config'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/server.rb:249:in `app'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/server.rb:422:in `wrapped_app'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/commands/server/server_command.rb:77:in `log_to_stdout'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/commands/server/server_command.rb:37:in `start'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/commands/server/server_command.rb:144:in `block in perform'
    from <internal:kernel>:90:in `tap'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/commands/server/server_command.rb:135:in `perform'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/command/base.rb:69:in `perform'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/command.rb:50:in `invoke'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/railties-6.1.3.1/lib/rails/commands.rb:18:in `<main>'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `block in require'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:299:in `load_dependency'
    from /Users/mj068550/.rvm/gems/ruby-3.0.0/gems/activesupport-6.1.3.1/lib/active_support/dependencies.rb:332:in `require'
    from bin/rails:8:in `<main>'

用户.rb

class User < ApplicationRecord
  extend Devise::Models

  devise :saml_authenticatable
  rolify before_add: :assign_child_roles
  rolify after_add: :remove_requested_role

  validates :name, presence: true, uniqueness: true

  after_create :assign_default_role

  private

  # Private - Assigns child role if "role" is a parent role. This reduces code clutter by removing role "or" conditions.
  #
  # role    - Role object.
  def assign_child_roles(role)
    Role::PARENT_ROLES[role.name.to_sym].to_a.each { |r| add_role(r, role.resource) unless r == role.name }
  end

  # Private - Assigns the default basic role to the user and starts the
  #           job to update roles of all users based on LDAP groups.
  def assign_default_role
    add_role(:basic)
  end

  # Public - Will add child roles to user as child roles are added to roles.
  def refresh_roles
    roles.includes([:resource]).each { |role| assign_child_roles(role) }
  end

  # Private - removes any existing [NOT APPROVED] versions of the role that's being added.
  #
  # role    - role that was just added. Is passed implicitly.
  def remove_requested_role(role)
    remove_role("#{role.name} [NOT APPROVED]", role.resource)
  end
end

角色.rb

class Role < ApplicationRecord
  has_and_belongs_to_many :users, join_table: :users_roles

  belongs_to :resource, polymorphic: true, touch: true

  validates :resource_type, inclusion: { in: Rolify.resource_types }, allow_nil: true
  validates :name, presence: true

  # Defines roles users will get if added to the key role.
  #   * For example, if a user were to be added as an 'approver',
  #     they'd automatically be added to the following array of roles within that same scope.
  #   * Admin is parent of all roles.
  PARENT_ROLES = {
    admin: Role.pluck(:name).uniq - ['admin'],
    approver: [:user],
    builder: %i[reclaimer user],
    moderator: %i[builder reclaimer user],
    network_admin: [:network_api]
  }.freeze

  scopify
end

客户端.rb

class Client < ApplicationRecord
  resourcify
end

如果我注释掉 Client.rb 中的 resourcecify 行和 User.rb 中的两个 rolify 行,我就能够启动服务器。使用 ruby​​ 2.7.2 时,服务器也运行良好。

4

1 回答 1

2

感谢 tolas 在 reddit 上提出的有用问题(我的队友也在那里发布了这个问题)。https://www.reddit.com/r/rubyonrails/comments/mmad7p/upgrading_ruby_to_30_and_rails_to_61_troubles/

“你的 rolify gem 升级到最新版本了吗?”

结果 rolify 被锁定为次要版本~> 5.1.0,因此将 gem 升级到~> 5.3Gemfile 并运行包更新修复了这个问题。

于 2021-04-08T14:58:14.387 回答