# Collection.build or collection.create gives "ArgumentError: wrong number of arguments (1 for 0)"

**URL:** <https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600>\
**Category:** Rails\
**Created:** [November 29, 2007, 6:48am UTC](https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600 "2007-11-29T06:48:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 29, 2007, 6:48am UTC](https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600/1 "2007-11-29T06:48:34Z")

</div>

I have a Project that has\_many Tasks. Tasks belongs to a Project. When  
I try the following:

project = Project.new  
project.tasks.build

I get:

> > s = Project.new  
> > =\> #\<Project:0x25f9e28 @attributes={“name”=\>"", “end\_at”=\>nil,  
> > “updated\_at”=\>nil, “published\_at”=\>nil, “user\_id”=\>nil,  
> > “created\_at”=\>nil}, @new\_record=true\>
> 
> > s.tasks.build  
> > ArgumentError: wrong number of arguments (1 for 0)  
> > from /Applications/Locomotive2/Bundles/  
> > standardRailsMar2007.locobundle/i386/lib/ruby/gems/1.8/gems/  
> > activerecord-1.15.3/lib/active\_record/associations/  
> > has\_many\_association.rb:13:in `initialize' from /Applications/Locomotive2/Bundles/ standardRailsMar2007.locobundle/i386/lib/ruby/gems/1.8/gems/ activerecord-1.15.3/lib/active_record/associations/ has_many_association.rb:13:in`new’  
> > from /Applications/Locomotive2/Bundles/  
> > standardRailsMar2007.locobundle/i386/lib/ruby/gems/1.8/gems/  
> > activerecord-1.15.3/lib/active\_record/associations/  
> > has\_many\_association.rb:13:in `build’  
> > from (irb):2

Am I doing something stupid? I would expect this to work.

Thanks,

Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 29, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600/2 "2007-11-29T17:59:45Z")

</div>

On Nov 28, 2007 11:48 PM, Scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a Project that has\_many Tasks. Tasks belongs to a Project. When  
> I try the following:
> 
> project = Project.new

You need to do project.save before adding any tasks. Otherwise how  
will ActiveRecord know what project\_id to record for a given task?

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 30, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600/3 "2007-11-30T18:58:04Z")

</div>

class Project \< ActiveRecord::Base  
belongs\_to :user  
has\_many :tasks  
end

class Task \< ActiveRecord::Base  
belongs\_to :project  
belongs\_to :task\_type  
end

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 29, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600/4 "2007-11-29T18:40:15Z")

</div>

On Nov 29, 2007 12:48 AM, Scott [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> #\<Project:0x25f9e28 @attributes={“name”=\>“”, “end\_at”=\>nil,  
> “updated\_at”=\>nil, “published\_at”=\>nil, “user\_id”=\>nil,  
> “created\_at”=\>nil}, @new\_record=true\>
> 
> > > s.tasks.build  
> > > ArgumentError: wrong number of arguments (1 for 0)  
> > > from /Applications/Locomotive2/Bundles/  
> > > standardRailsMar2007.locobundle/i386/lib/ruby/gems/1.8/gems/  
> > > activerecord-1.15.3/lib/active\_record/associations/  
> > > has\_many\_association.rb:13:in `initialize’

Post your models. Possibly has\_many declaration is wrong, or Task is  
not derived from ActiveRecord::Base? Does

Task.new

give expected results from the console?

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [November 30, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/collection-build-or-collection-create-gives-argumenterror-wrong-number-of-arguments-1-for-0/121600/5 "2007-11-30T19:42:32Z")

</div>

From the rdoc on has\_many:

collection.build(attributes = {}) - returns a new object of the  
collection type that has been instantiated with attributes and linked  
to this object through a foreign key but has not yet been saved.  
_Note:_ This only works if an associated object already exists, not if  
it’s nil!

You shouldn’t have to save it… that’s the whole point.
