# Nested attributes requires an array and throws a TypeError if passed a hash

**URL:** <https://www.ruby-forum.com/t/nested-attributes-requires-an-array-and-throws-a-typeerror-if-passed-a-hash/236517>\
**Category:** Rails\
**Created:** [September 10, 2013, 1:53pm UTC](https://www.ruby-forum.com/t/nested-attributes-requires-an-array-and-throws-a-typeerror-if-passed-a-hash/236517 "2013-09-10T13:53:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 10, 2013, 1:53pm UTC](https://www.ruby-forum.com/t/nested-attributes-requires-an-array-and-throws-a-typeerror-if-passed-a-hash/236517/1 "2013-09-10T13:53:13Z")

</div>

Below is an example of the error I encountered in my project today and  
after a great deal of searching I realized the problem to be that the \*  
\_attributes needs to be an array! My first question is, is there a use  
for  
\*\_attributes={} and is there something I’m missing to make it work in  
that  
case? If not, then what _should_ the behavior be? Should it be allowed  
and instead create one record (a letter for example)? Thanks.

Setup a simple rails app:

class Word \< ActiveRecord::Base  
has\_many :letters, dependent: :destroy  
accepts\_nested\_attributes\_for :letters  
end

class Letter \< ActiveRecord::Base  
belongs\_to :word  
end

This works just fine (e.g. in the console):

attrs = { ‘letters\_attributes’ =\> [{ ‘test’ =\> ‘1’ }] }  
Word.new attrs

But this give an error:

attrs = { ‘letters\_attributes’ =\> { ‘test’ =\> ‘1’ } }  
Word.new attrs

and the output looks something like:

TypeError: no implicit conversion of Symbol into Integer  
from /…/active\_record/nested\_attributes.rb:452:in `[]' from /../active_record/nested_attributes.rb:452:in`block in  
assign\_nested\_attributes\_for\_collection\_association’  
from /…/active\_record/nested\_attributes.rb:452:in `map' from /../active_record/nested_attributes.rb:452:in`assign\_nested\_attributes\_for\_collection\_association’  
from /…/active\_record/nested\_attributes.rb:339:in  
`letters_attributes=' from /../active_record/attribute_assignment.rb:42:in`public\_send’  
from /…/active\_record/attribute\_assignment.rb:42:in  
`_assign_attribute' from /../active_record/attribute_assignment.rb:53:in`block in  
assign\_nested\_parameter\_attributes’  
from /…/active\_record/attribute\_assignment.rb:53:in `each' from /../active_record/attribute_assignment.rb:53:in`assign\_nested\_parameter\_attributes’  
from /…/active\_record/attribute\_assignment.rb:33:in  
`assign_attributes' from /../active_record/core.rb:192:in`initialize’  
from /…/active\_record/inheritance.rb:27:in `new' from /../active_record/inheritance.rb:27:in`new’

---

<div class="post-metadata">

**Author:** ![Brett\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@Brett\_P](https://www.ruby-forum.com/u/Brett_P)\
**Post date:** [September 10, 2013, 4:55pm UTC](https://www.ruby-forum.com/t/nested-attributes-requires-an-array-and-throws-a-typeerror-if-passed-a-hash/236517/2 "2013-09-10T16:55:28Z")

</div>

On Monday, September 9, 2013 11:16:14 PM UTC+1, Brett P. wrote:

> Below is an example of the error I encountered in my project today and  
> after a great deal of searching I realized the problem to be that the \*  
> \_attributes needs to be an array! My first question is, is there a use  
> for \*\_attributes={} and is there something I’m missing to make it work in  
> that case? If not, then what _should_ the behavior be? Should it be  
> allowed and instead create one record (a letter for example)? Thanks.
> 
> it should either be an array or a hash where the keys are indices e.g.

{‘1’ =\> {‘test’ =\> ‘foo’}, ‘2’ =\> {‘test’ =\> ‘bar’}, …}

The expected format is  
documented:

> **[ActiveRecord::NestedAttributes#assign\_nested\_attributes\_for\_collection\_associati...](https://apidock.com/rails/v3.2.13/ActiveRecord/NestedAttributes/assign_nested_attributes_for_collection_association)**
>
> ActiveRecord::NestedAttributes#assign\_nested\_attributes\_for\_collection\_association - Ruby on Rails API documentation. View source code and usage examples.

The keys in this top level hash are irrelevant

> 

Fred

Setup a simple rails app:

---

<div class="post-metadata">

**Author:** ![Brett\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@Brett\_P](https://www.ruby-forum.com/u/Brett_P)\
**Post date:** [September 10, 2013, 10:38pm UTC](https://www.ruby-forum.com/t/nested-attributes-requires-an-array-and-throws-a-typeerror-if-passed-a-hash/236517/3 "2013-09-10T22:38:21Z")

</div>

On Sep 9, 2013, at 5:16 PM, Brett P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Below is an example of the error I encountered in my project today and after a  
> great deal of searching I realized the problem to be that the \*\_attributes needs  
> to be an array! My first question is, is there a use for \*\_attributes={} and is  
> there something I’m missing to make it work in that case? If not, then what  
> should the behavior be? Should it be allowed and instead create one record (a  
> letter for example)? Thanks.
> 
> Setup a simple rails app:
> 
> class Word \< ActiveRecord::Base  
> has\_many :letters, dependent: :destroy

Since you told it there would be many letters in the word, it only seems  
to make sense that ‘letters\_attributes’ would an Array, even if it only  
has one item in it.

---

<div class="post-metadata">

**Author:** ![Brett\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@Brett\_P](https://www.ruby-forum.com/u/Brett_P)\
**Post date:** [September 25, 2013, 9:44pm UTC](https://www.ruby-forum.com/t/nested-attributes-requires-an-array-and-throws-a-typeerror-if-passed-a-hash/236517/4 "2013-09-25T21:44:46Z")

</div>

I see now. Since it can be hash it’s looking trying to convert ‘test’  
(in  
my hash) into an integer. Thanks.

–Brett
