gpt4 book ai didi

ruby-on-rails - Rails 2.4 => 3.0 : ActiveRecord: `add_modifier` : wrong number of arguments (given 3, 预期 2) (ArgumentError)

转载 作者:行者123 更新时间:2023-12-05 00:52:44 28 4
gpt4 key购买 nike

当我将 Ruby 版本从 2.4.3 更新到 3.0.2 (bundle update) 并尝试运行 Rails 控制台时,我收到此错误消息。我该如何解决这个问题?

C:\Users\Chloe\workspace\catalyst_research>rails console
C:/ruby30/lib/ruby/gems/3.0.0/gems/activerecord-5.0.7.2/lib/active_record/type/adapter_specific_registry.rb:7:in `add_modifier': wrong number of arguments (given 3, expected 2) (ArgumentError)
from C:/ruby30/lib/ruby/gems/3.0.0/gems/activerecord-5.0.7.2/lib/active_record/type.rb:22:in `add_modifier'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/postgresql_adapter.rb:827:in `<class:PostgreSQLAdapter>'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/postgresql_adapter.rb:69:in `<module:ConnectionAdapters>'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `<module:ActiveRecord>'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/activerecord-5.0.7.2/lib/active_record/connection_adapters/postgresql_adapter.rb:19:in `<main>'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/bootsnap-1.9.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
from C:/ruby30/lib/ruby/gems/3.0.0/gems/activesupport-5.0.7.2/lib/active_support/dependencies.rb:293:in `block in require'
...
C:/Users/Chloe/workspace/catalyst_research/config/environment.rb:5:in
...

gem 文件:

ruby '~>2.4.1'#'~>3.0.2'
gem 'rails', '~> 5.0.2'
...

environment.rb:5:

Rails.application.initialize!

adapter_specific_registry.rb:7:

  6     class AdapterSpecificRegistry < ActiveModel::Type::Registry
7 def add_modifier(options, klass, **args)
8 registrations << DecorationRegistration.new(options, klass, **args)
9 end

最佳答案

Rails 5.0.X 与 Ruby 3.0 不兼容 - 期间。这不会因为不再维护而改变。

Rails 6.0.2是最早兼容 Ruby 3.0 的 Rails 版本。

Ruby 3.0 有一些重大变化。尤其是 separation of keyword arguments from positional arguments这是您遇到错误的最可能原因。

在以前的 Ruby 版本中,您可以将方法称为:

add_modifier({ foo: 1 }, klass, { bar : 1 })

Ruby 会自动将最后一个位置参数转换为关键字参数。这已被弃用,将在 Ruby 2.7 中发出警告,并在 Ruby 3.0 中完全删除。您得到(给定 3,预期 2),因为最后一个参数被视为位置参数。

如果你想在 Ruby 3 中将哈希作为关键字参数传递,你需要使用双 splat 来转换它:

add_modifier({ foo: 1 }, klass, **{ bar : 1 })

TLDR

你需要找到一个不同的upgrade path当您将项目升级到新版本的 Rails 时,您会逐步升级 Ruby 版本。

关于ruby-on-rails - Rails 2.4 => 3.0 : ActiveRecord: `add_modifier` : wrong number of arguments (given 3, 预期 2) (ArgumentError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69274304/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com