As I learned update_only doesn’t work for has_many association.
I am updating nested attributes and it creates new children each time and don’t delete old children. What would be the good way to delete them automatically on each update (to mimic :update_only behavior which works for has_one)?
I know about :allow_destroy. However, I don’t have children id’s to let rails know what should be destroyed.
P.S. I asked this question originally back in 2012 and back then I found a hacky solution to override assign_nested_attributes_for_collection_association. I wonder whether anything was introduced in Rails to solve this problem. It looks like very common straightforward case. I wonder why there is a built-in solution for it.
The best approach which I found is to override method assign_nested_attributes_for_collection_association on the object, check association type in there. if it’s has_many then do destroy_all on this association.
Such code can be generalized and moved to a module for further reuse.