Donald Ball
2009-Apr-11 22:01 UTC
odd behavior with accepts_nested_attributes_for on a belongs_to association
A fairly common UI requirement is for a user to be able to choose an existing object or create a new one, e.g. select or supply an address for a shipment. The accepts_nested_attributes_for code goes some way towards supporting this use case, but falls short in a couple of areas. The basic problem is that the parameters hash might include both an ''address_id'' value (from a select box of existing addresses from which to choose) and an ''address_attributes'' value (from the embedded model form), and the behavior of accepts_nested_attributes_for in this situation is not well-defined and is in some cases sensitive to the order in which the hash keys are processed. I''ve written up a simple rails app with a unit test suite which defines the current behavior and exposes at least a few oddities therein: http://github.com/dball/nested_test/tree/master There are probably some other corner cases I failed to consider in this test suite, but it''s at least a start. I am working around the problem at least partially in my controllers by massaging the parameters before sending them to the model, but a more general solution is probably worthwhile. I figured I''d check here to see if others agree before digging in and trying to patch active_record itself. - donald --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Eloy Duran
2009-Apr-15 07:07 UTC
Re: odd behavior with accepts_nested_attributes_for on a belongs_to association
Donald, Yes, please create a patch. Thanks, Eloy On Apr 12, 2009, at 12:01 AM, Donald Ball wrote:> > A fairly common UI requirement is for a user to be able to choose an > existing object or create a new one, e.g. select or supply an address > for a shipment. The accepts_nested_attributes_for code goes some way > towards supporting this use case, but falls short in a couple of > areas. The basic problem is that the parameters hash might include > both an ''address_id'' value (from a select box of existing addresses > from which to choose) and an ''address_attributes'' value (from the > embedded model form), and the behavior of > accepts_nested_attributes_for in this situation is not well-defined > and is in some cases sensitive to the order in which the hash keys are > processed. > > I''ve written up a simple rails app with a unit test suite which > defines the current behavior and exposes at least a few oddities > therein: > > http://github.com/dball/nested_test/tree/master > > There are probably some other corner cases I failed to consider in > this test suite, but it''s at least a start. > > I am working around the problem at least partially in my controllers > by massaging the parameters before sending them to the model, but a > more general solution is probably worthwhile. I figured I''d check here > to see if others agree before digging in and trying to patch > active_record itself. > > - donald > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---