Brett Pontarelli
2013-Sep-09 22:16 UTC
Nested attributes requires an array and throws a TypeError if passed a hash.
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'' -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3c1c3a19-4a8d-4c95-ac4c-6395a055fdff%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Sep-10 14:54 UTC
Re: Nested attributes requires an array and throws a TypeError if passed a hash.
On Monday, September 9, 2013 11:16:14 PM UTC+1, Brett Pontarelli 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: http://apidock.com/rails/v3.2.13/ActiveRecord/NestedAttributes/assign_nested_attributes_for_collection_association The keys in this top level hash are irrelevant>Fred 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'' > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c7b0dae1-40e8-47f8-a1cd-a1e7ae93f7d4%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Tamara Temple
2013-Sep-10 20:36 UTC
Re: Nested attributes requires an array and throws a TypeError if passed a hash.
On Sep 9, 2013, at 5:16 PM, Brett Pontarelli <brett-X2aoe8tVq3Y3GDxJruMu3g@public.gmane.org> 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: :destroySince 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.> 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'' > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3c1c3a19-4a8d-4c95-ac4c-6395a055fdff%40googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/68B6BD6E-B853-4E9F-8203-C5127E73E5B8%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Brett Pontarelli
2013-Sep-25 19:42 UTC
Re: Nested attributes requires an array and throws a TypeError if passed a hash.
I see now. Since it can be hash it''s looking trying to convert ''test'' (in my hash) into an integer. Thanks. --Brett On Tuesday, September 10, 2013 7:54:15 AM UTC-7, Frederick Cheung wrote:> > > > On Monday, September 9, 2013 11:16:14 PM UTC+1, Brett Pontarelli 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: > http://apidock.com/rails/v3.2.13/ActiveRecord/NestedAttributes/assign_nested_attributes_for_collection_association > The keys in this top level hash are irrelevant > >> > Fred > > 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'' >> >>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/65b65286-98e8-4d2d-b8bf-53c307d4f5a5%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.