acts_as_votable
acts_as_votable copied to clipboard
Duplicates (race condition)
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
- Add a unique key in votes table.
- Get unique by user count votes for post (but I don't know how), some like
@post.votes_for.up.unique_by(User)
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.
Maybe just disable the vote button with js when clicked.
@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 Yes!
I relied on the fact that the unique key was added during installation
generate acts_as_votable:migration
@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 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 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?
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