acts_as_votable icon indicating copy to clipboard operation
acts_as_votable copied to clipboard

Duplicates (race condition)

Open floydback opened this issue 9 years ago • 8 comments

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)

floydback avatar Sep 16 '15 08:09 floydback

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.

joshmn avatar Dec 30 '15 22:12 joshmn

Maybe just disable the vote button with js when clicked.

adriandelarco avatar May 21 '16 11:05 adriandelarco

@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.

laertispappas avatar Sep 05 '17 14:09 laertispappas

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

floydback avatar Sep 05 '17 22:09 floydback

@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.

laertispappas avatar Sep 06 '17 18:09 laertispappas

@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

floydback avatar Sep 06 '17 18:09 floydback

@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?

laertispappas avatar Sep 07 '17 08:09 laertispappas

Just chiming in here: Allowing users to vote on specific features is one of the ways I have seen the "multiple votes per *thing *per user" implemented. UserVoice is an example, though I suspect they might have a value column on a record to indicate how many "votes" or "points" were awarded to an individual thing from a specific user.

On Thu, Sep 7, 2017 at 3:16 AM, Laertis [email protected] wrote:

@floydback https://github.com/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 though.

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ryanto/acts_as_votable/issues/127#issuecomment-327725696, or mute the thread https://github.com/notifications/unsubscribe-auth/AB7ykLwy0x10Zr1LJx61Nyj7_zs5r5ttks5sf6ZXgaJpZM4F-PAC .

--

Hello from Minneapolis Josh Brody (952) 239-7408 www http://www.josh.mn

joshmn avatar Sep 07 '17 08:09 joshmn