# Simple counting problem (inside rOrails controller)

**URL:** <https://www.ruby-forum.com/t/simple-counting-problem-inside-rorails-controller/66895>\
**Category:** Ruby\
**Created:** [August 30, 2006, 4:11am UTC](https://www.ruby-forum.com/t/simple-counting-problem-inside-rorails-controller/66895 "2006-08-30T04:11:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [August 30, 2006, 4:11am UTC](https://www.ruby-forum.com/t/simple-counting-problem-inside-rorails-controller/66895/1 "2006-08-30T04:11:15Z")

</div>

I want to count, perhaps with session variables, to ensure that the  
user is not abusing AJAX features.

so on the particular page that has the ajax

unless session[:countcomment] !=nil  
session[:countcomment] = 0.to\_i  
end

then, i check to see if the session[:countcomment] is greater than 1,  
if it is, then no other ajax submissions are allowed, I tried to do  
this with the following code:

if session[:countcomment].to\_i \> 1.to\_i  
if thiscomment.save  
session[:countcomment] = session[:countcomment].to\_i + 1.to\_i  
render some text  
end  
else  
render\_text “too many posts”  
end

Except, this isn’t working. Can variables be declared in Ruby? Is this  
a poor approach to trying to limit AJAX activity?

---

<div class="post-metadata">

**Author:** ![jotto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/eada6e/32.png) [@jotto](https://www.ruby-forum.com/u/jotto)\
**Post date:** [August 30, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/simple-counting-problem-inside-rorails-controller/66895/2 "2006-08-30T15:41:13Z")

</div>

jotto wrote:  
…

> unless session[:countcomment] !=nil  
> session[:countcomment] = 0.to\_i  
> end

I would avoid the double negative:  
session[:countcomment] = 0.to\_i if session[:countcomment].nil?  
…

> if session[:countcomment].to\_i \> 1.to\_i  
> if thiscomment.save  
> session[:countcomment] = session[:countcomment].to\_i + 1.to\_i  
> render some text  
> end  
> else  
> render\_text “too many posts”  
> end

The logic here is saving the comment if the count is greater than 1?  
The opposite of what you want.

Also, why all the to\_i calls?

Cheers
