[英]wrong number of arguments (given 4, expected 0..1) after upgrading to Rails 6
I just upgraded from Rails 5.2 to Rails 6 and I'm facing an issue in one class.我刚刚从 Rails 5.2 升级到 Rails 6,我在一个课程中遇到了一个问题。
class Calculator < ApplicationRecord
def initialize(obj, user_id, calc_type, will_id = nil )
@inheritors = obj
super_obj = obj.clone.merge!(user_id: user_id, type: calc_type, will_id: will_id)
super(super_obj)
@shares = {}
@remains = RationalWithArgumentStore(0)
@stop_residual_shares = false
@special_case_apply = false
@rules = {}
@authorities = {}
end
end
and I'm creating new instance of the class like the following我正在创建类的新实例,如下所示
calc = Calculator.new(obj, user_id, calc_type, nil)
It through an error saying:它通过一个错误说:
wrong number of arguments (given 4, expected 0..1)
I spent two days searching and trying to fix the issue without any luck.我花了两天时间搜索并试图解决这个问题,但没有任何运气。 Your help is highly appreciated
非常感谢您的帮助
This is happening because you are redefining initialize
for a class that inherits from ApplicationRecord
.发生这种情况是因为您正在为继承自
ApplicationRecord
的类重新定义initialize
。 It throws an error here .它在这里抛出一个错误。 If you were to do the following it would work.
如果您要执行以下操作,它将起作用。
class Calculator
def initialize(obj, user_id, calc_type, will_id = nil)
@obj = obj
@user_id = user_id
@calc_type = calc_type
@will_id = will_id
end
end
Note that redefining initialize
in this way is not recommended (see docs here ), so if you can I would look into callbacks and see if you can accomplish your goal with that.请注意,不建议以这种方式重新定义
initialize
(请参阅此处的文档),因此,如果可以,我会研究回调,看看您是否可以用它来实现您的目标。
Hope that helps.希望有帮助。
(Rails 6.x) One of my models had include Rails.application.routes.url_helpers
. (Rails 6.x)我的一个模型
include Rails.application.routes.url_helpers
。
If you have the same problem, remove it and manage your url methods by calling it directly as:如果您有同样的问题,请将其删除并通过直接调用它来管理您的 url 方法:
def my_link_function
# ... my code
my_link = Rails.application.routes.url_helpers.url_for([my_params]))
# ... other code
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.