Forum: Ruby on Rails ActiveRecord increment confusion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5888fc25101419e40b7de521f8524dad?d=identicon&s=25 Aaron Suggs (ktheory)
on 2006-03-11 16:53
Part of my application counts the number of times a link was clicked.
The Link.click function should increment the @link.clicks attribute.

I assumed

  def click
    @link = Link.find(params[:id])
    if @link.increment('clicks')
      redirect_to @link.url
    else
      flash[:error] = 'Could not update clicks'
      redirect_to :action => 'list'
    end
  end

would do the trick. But I can't get @link.increment to work. Instead

    if @link.update_attribute('clicks', @link.clicks+1)

does the trick, but it's a cludge.

I'm just learning Ruby and I'm still wrapping my head around the proper
use of attributes. I'm sure I'm making some stupid mistake with the
@link.increment line. If someone could point it out, I'd be grateful.
:-)

Cheers!
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 Adam Denenberg (Guest)
on 2006-03-11 17:46
(Received via mailing list)
I think all you need to do is

  Link.increment_counter("clicks",@params[:id])

adam
5888fc25101419e40b7de521f8524dad?d=identicon&s=25 Aaron Suggs (ktheory)
on 2006-03-11 20:37
Yay! Thanks!

-Aaron
This topic is locked and can not be replied to.