I have an app where any user (no login required) can vote on something and I need a way to limit them to one vote per session. Does anyone have any suggestions?
Thanks a lot for any help you can offer.
I have an app where any user (no login required) can vote on something and I need a way to limit them to one vote per session. Does anyone have any suggestions?
Thanks a lot for any help you can offer.
Off the top of my head:
class VoteManager
def initialize(session) @session = session end
def submit(*args) unless @session[:voted] vote = Vote.create!(*args) @session[:voted] = true vote end end end # in controller
vote_manager = Vote.new(session) vote_manager.submit(params[:vote]) # will return nil if voted before
If you use activerecord sessions (you should be doing that) you can have a session model and save the vote with a session_id.
The thing is that you are going to prune the sessions table (you should be doing that too) and they are going to be nullified, but if you keep sessions for 2 weeks is not a problem having votes without a session assigned.
Disclaimer: i haven't done anything like this but i think it may for you.
Online voting is fundamentally flawed (sessions/cookies can be circumvented, everything can be actually), but i’ve been using a combination of sessionbased, cookiebased and ip based filtering.
Best regards
Peter De Berdt