# One Vote Per Session

**URL:** <https://www.ruby-forum.com/t/one-vote-per-session/126443>\
**Category:** Rails\
**Created:** [January 18, 2008, 10:15am UTC](https://www.ruby-forum.com/t/one-vote-per-session/126443 "2008-01-18T10:15:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![apjanes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@apjanes](https://www.ruby-forum.com/u/apjanes)\
**Post date:** [January 18, 2008, 10:15am UTC](https://www.ruby-forum.com/t/one-vote-per-session/126443/1 "2008-01-18T10:15:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![apjanes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@apjanes](https://www.ruby-forum.com/u/apjanes)\
**Post date:** [January 18, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/one-vote-per-session/126443/2 "2008-01-18T14:22:42Z")

</div>

On Jan 18, 2008 9:15 AM, Aaron J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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.

---

<div class="post-metadata">

**Author:** ![apjanes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@apjanes](https://www.ruby-forum.com/u/apjanes)\
**Post date:** [January 18, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/one-vote-per-session/126443/3 "2008-01-18T13:28:05Z")

</div>

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

On Jan 18, 4:15 am, Aaron J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![apjanes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@apjanes](https://www.ruby-forum.com/u/apjanes)\
**Post date:** [January 18, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/one-vote-per-session/126443/4 "2008-01-18T14:39:16Z")

</div>

On 18 Jan 2008, at 14:21, Emilio T. wrote:

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

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
