Update Query - Adding one to a int value

I have the following command on a controller
def increasecount
Item.update (1, {:count=>‘10’} )
end

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} )
end

Is it possible to make a method of this and then call it from other
views?

On 2/11/07, Ranjith V. [email protected] 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
end
end

Then call @item.increment_count! as you please.

jeremy

Hi jeremy,

Thanks for the tip!

I have a html page which was calling a javascript. Here is the snippet
below:

Choice1

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

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

belongs_to:Question

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

end

Where do I substitute the javascript function call with the ruby method
call?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs