问题描述
尝试创建模型的实例时,出现以下错误...
Trying to create an instance of a model, I get the following error...
u = User.create
# or .where(...).first_or_create
# or .where(...).first_or_initialize
ArgumentError: wrong number of arguments (2 for 1)
有人在使用 Rails 3.2 时遇到同样的问题吗?
Is anyone having the same problem with Rails 3.2?
推荐答案
您是否重载了模型的 initialize
方法?在我的情况下,我已经重载了:
Have you overloaded your model's initialize
method? In my case, I had overloaded it with:
def initialize(attributes=nil)
...
end
我必须解决的问题:
def initialize(attributes = nil, options = {})
...
end
在 Rails 3.2 中,提交 7c5ae0a88fc9406857ee362c827c57eb23fd5f95(为 AR.new 添加了批量赋值安全性 :as 和 :without_protection 支持)为上述方法添加了更多参数,这就是我之前的实现失败的原因.
In Rails 3.2, the commit 7c5ae0a88fc9406857ee362c827c57eb23fd5f95 (Added mass-assignment security :as and :without_protection support to AR.new) added more arguments to the above method and that's why my previous implementation was failing.
这篇关于Rails 3.2:ArgumentError:创建时参数数量错误(2 为 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!