Rails 3.2: ArgumentError: wrong number of arguments (2 for 1) on create

爷,独闯天下 提交于 2019-12-22 04:31:37

问题


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)

Is anyone having the same problem with Rails 3.2?


回答1:


Have you overloaded your model's initialize method? In my case, I had overloaded it with:

def initialize(attributes=nil)
    ...
end

Which I had to fix to:

def initialize(attributes = nil, options = {})
    ...
end

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.



来源:https://stackoverflow.com/questions/8942728/rails-3-2-argumenterror-wrong-number-of-arguments-2-for-1-on-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!