问题
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