FIX: Prevent from creation of duplicated TopicAllowedUsers (PR #8169)

Something changed in Rails6 and now it behaves correctly. When we try to build a collection object with the same params it creates duplication which later we try to save.

Besides, building that object in the loop when we are doing the same thing 2 lines later is not necessary. (please extend file to see it)

Related to https://meta.discourse.org/t/error-message-topic-allowed-users-is-invalid/130382/5

Screenshot from 2019-10-08 15-42-00

GitHub

You’ve signed the CLA, lis2. Thank you! This pull request is ready for review.

This is adding user.id not @user.id this is the user associated with the post.

So instead, keep track of all the new users in a hash or array and do the addition the end.

users = {}
if ...  
     # allows for deleted users
     user = users[p.user_id] ||= User.find_by(id: p.user_id)
     if user
       user.user_stat...
     end
end

finally ensuring al these new users exist in topic allowed users...

You are right, my bad. What do you think of instead of building a hash of users, remove duplications just before creation?

does this fix it? Can you add a failing test case here?

@SamSaffron fixed - https://github.com/discourse/discourse/pull/8169/commits/5244b3d0e4784606c92e2fed4e1f2f1a273524bf

OK lets give this a shot!