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:

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


Yes, please create a patch.

Thanks, Eloy