Help With Update method

On Dec 31, 2007, at 11:07 AM, Mark W. wrote:

module ActiveRecord
class Base
def change_value(attribute, amount)
self[attribute] += amount
end

This looks good (some of the calls to change_value should be calling
change_value!), but I think it’s too close to existing functionality
(update_counters).

Actually, there are two patches in for this. The tickets are:

10542 (not mine)

and

10656 (mine)

The primary difference is syntactic, but 10542 is already verified,
and thus may be the patch that is accepted. I’ve asked for a
discussion about the syntax on
<http://dev.rubyonrails.org/ticket/10542#comment:7

. The other patch allows for syntax like:

@player.increment! :score, 3

My suggested patch allows for a slightly different syntax:

@player.increment! :score, :by => 3

it also opens the door to future enhancements like:

@player.increment! :score, :by => 3, limit => 100, :if => :positive

I’m not sure there’s a use case for anything too complex in increment
and decrement, though :slight_smile: