Forum: Ruby on Rails Update Query - Adding one to a int value

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Fa57f9c8e5dba90389245270d5547f44?d=identicon&s=25 Ranjith Venkatesh (ranjithvenkatesh)
on 2007-02-11 10:34
I have the following command on a controller
def increasecount
   Item.update (1, {:count=>'10'} )

I am able to call this in a view 'increasecount.rhtml'

I need to do the following:
Increase the count when a link is accessed in the application

Things which I tried and does not work:
def increasecount
   Item.update (1, {:count=>count+1} )

Is it possible to make a method of this and then call it from other
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2007-02-11 10:47
(Received via mailing list)
On 2/11/07, Ranjith Venkatesh <> wrote:
> views?
Active Record provides you with
  Item.increment_counter :count, 1234
which performs 'update items set count=count+1 where id=1234'

You may wrap that in an instance method such as
  class Item < AR::Base
    def increment_count!
      self.class.increment_count :count, id

Then call @item.increment_count! as you please.

Fa57f9c8e5dba90389245270d5547f44?d=identicon&s=25 Ranjith Venkatesh (ranjithvenkatesh)
on 2007-02-11 11:35
Hi jeremy,

Thanks for the tip!

I have a html page which was calling a javascript. Here is the snippet
<div style="float:left;margin-left:10px;">
<a id='Choice1Image'
onfocus="toggleImage(" onblur="toggleImageBack(">
<img name='button'  alt='Choice1' src="/images/choice1.gif"/>

I need to call the increment when the user click this link.

My Choice class looks like this now:
class Choice < ActiveRecord::Base


    def increment_count!
      self.class.increment_counter :count, id


Where do I substitute the javascript function call with the ruby method
This topic is locked and can not be replied to.