Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicates (race condition) #127

Open
floydback opened this issue Sep 16, 2015 · 8 comments · May be fixed by #211
Open

Duplicates (race condition) #127

floydback opened this issue Sep 16, 2015 · 8 comments · May be fixed by #211

Comments

@floydback
Copy link

Hi!
In my project users votes every day a lot (about ~5000 votes). User can voted for post only one time.
So, I have a simple code

@post.liked_by current_user unless current_user.voted_for? @post

But. I've find duplicates in votes table. One user voted two or more times for the same post.
I think it's happened when the server is overloaded and user send two or more times one http request in a short time.

How to prevent this? I think about

  1. Add a unique key in votes table.
  2. Get unique by user count votes for post (but I don't know how), some like

@post.votes_for.up.unique_by(User)

@joshmn
Copy link

joshmn commented Dec 30, 2015

Could throw the vote in a background job and put a delay on it for a second.

Edit: really hacky but it'd do the job.

@adriandelarco
Copy link

Maybe just disable the vote button with js when clicked.

@laertispappas
Copy link

laertispappas commented Sep 5, 2017

@floydback You will definitely need a unique key if your business needs the user to vote only once. You should add a database constraint + rails validations of course and handle the user already voted scenario in you client.

@floydback
Copy link
Author

@laertispappas Yes!
I relied on the fact that the unique key was added during installation
generate acts_as_votable:migration

@laertispappas
Copy link

@floydback I am not sure I do understand you. At the time being as far as I know and based on the migration template no constraint is added. I think there should be one since it doesn't make sense to have multiple votes per option.

@floydback
Copy link
Author

@laertispappas Yes you are right. I have added a unique key to DB manually and it's work. Sorry for my English. All I want to say is when I installed this gem I expected migration file has some code like this:
add_index :votes, [:voter_id, :voter_type, :votable_id, :votable_type], unique: true
Maybe it would be nice to add in gem migration file

@laertispappas
Copy link

laertispappas commented Sep 7, 2017

@floydback Agree with you but this depends on the application. Maybe there are applications that do need to have multiple votes (although I can't find a use case). +1 for your suggestion.

By the way is you're application public? Could I have a look since I am interested?

@joshmn
Copy link

joshmn commented Sep 7, 2017 via email

@fatkodima fatkodima linked a pull request Nov 20, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants