After upgrading to Ruby 3, unable to pass multiple arguments to my "initialize" method

745 views Asked by At

I recently upgraded to Rails 6 with Ruby 3. I have this in a controller

my_object = MyObject.new(my_object_params, @header)

The object “initialize” method is defined like so

  def initialize(params, header)
    super(params)
    user&.header = header
  end

But now when attempting to initialize the object, I get the error

 ArgumentError:
   wrong number of arguments (given 2, expected 0..1)

What’s the proper way in Ruby 3 to pass multiple arguments to an object during initialization?

1

There are 1 answers

0
Kali Vara On

You need to do this way:

  def initialize(params, header)
    super(**params) #Note that you need to splat out the params!
    user&.header = header
  end