in this screencast Ryan Bates showed how to implement reputation system from scratch. But is there any way to give the current_user only 1 vote, and check whether user has already voted for application, and if yes, restrict the possibility of voting?
I think something like this in user.rb should work, but I don’t know ho to exactly write it
def has_one_vote
Restrict amount of user votes to 1
end
Thanks
I’m assuming you have Users, Votes, and Posts, and that users vote on posts.
You should add a uniqueness validator to the
Voteclass on theuser_idattribute, scoped to thepost_id. This limits the number of votes a user can have on a given post to one:To limit the total number of votes a user can ever create, either remove the
scopefrom the uniqueness validator, or (more correctly) move the foreign key into theuserstable.That is, either this:
or this: