I solved the problem differently and wanted to post a simplified version of this here if it helps someone else. In my real application, the association between both resources is HABTM, and the attached resource is the file attachment.
So, in the action of creating the controller, I separate the parameters for the source resource and those specified for the embedded resource.
Then I further divide the embedded resource into objects that exist in the database and objects that do not, putting the identifiers of those that do in the array.
If there are no existing nested objects, then it just floats from here.
However, if there are both existing and new nested objects, I create a new Item object this way:
@item = Item.new(:attachment_ids => existing_attachment_ids)
After that, I update @item like this:
@item.update_attributes(original_item_params)
@item.update_attributes(params_for_new_nested_objects)
You can then call @item.save and re-display the view if any errors occur.
I still cannot figure out if this is a bug or an ir Rails function. If anyone has thoughts on this issue or my decision, I would be very happy to hear them.
source share