Home > OS >  Rails 2.4 => 3.0: ActiveRecord: `add_modifier`: wrong number of arguments (given 3, expected 2) (
Rails 2.4 => 3.0: ActiveRecord: `add_modifier`: wrong number of arguments (given 3, expected 2) (

Time:09-23

When I update my Ruby version from 2.4.3 to 3.0.2, (bundle update), and try to run the Rails console, I get this error message. How can I resolve this?

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
...

Gemfile:

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

CodePudding user response:

Rails 5.0.X is not compatible with Ruby 3.0 - period. And that won't change as its no longer maintained.

Rails 6.0.2 is the earliest Rails version which is compatible with Ruby 3.0.

Ruby 3.0 has some significant changes. Especially the separation of keyword arguments from positional arguments which is the most likely cause of the error you're getting.

In previous versions of Ruby you could call the method as:

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

And Ruby would automatically convert the last positional argument to keyword arguments. This was deprecated and will issue a warning in Ruby 2.7 and was completely removed in Ruby 3.0. You're getting (given 3, expected 2) as the last argument is treated as a positional argument.

If you want to pass a hash as keyword arguments in Ruby 3 you need to use a double splat to convert it:

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

TLDR

You need to find a different upgrade path where you upgrade your Ruby version incrementally as you upgrade the project to newer versions of Rails.

  • Related