Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 739
Rafael Xavier Avatar asked Jan 20 '12 14:01

Rafael Xavier


1 Answers

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.

like image 109
Rafael Xavier Avatar answered Sep 23 '22 07:09

Rafael Xavier