Added test cases and a fix for rails/rails#10865.

Assuming Topic has_many Replies and Reply belongs_to Topic, the counter cache
was incremented twice if reply is saved after reassignment, as follows:

    reply.topic = topic  # triggers BelongsToAssocation#replace
    reply.save           # triggers belongs_to_counter_cache_after_update

The problem is caused by duplicate work.

This commit adds a flag to the reply if it has already triggered an increment,
but has not been saved yet. Thus, when the reply is actually saved, the
superfluous increment does not occur.

Note the following edge case:

    reply.topic = t1    # not saved, but counter updated
    t2.replies << t2    # save! called, previous change needs to be undone