Remove unnecessary db call when replacing.

When replacing a has_many association with the same one, there is no
need to do a round-trip to the db to create/and drop a new transaction.

[fixes #14220]
Replace map.flatten(1) with flat_map
Merge pull request #14240 from sferik/flat_map

Replace map.flatten(1) with flat_map
Merge pull request #14234 from arthurnn/fix_trans_on_replace

Remove unnecessary db call when replacing.