ActiveRecord extension to allow for updating numerical attributes using deltas.
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
MIT-LICENSE
Manifest
README.markdown
Rakefile
ar-deltas.gemspec

README.markdown

ActiveRecord Delta Updates

model = Widget.find(1)
puts model.number #=> 0
model.number += 5
model.save

# Before:
UPDATE `widgets` SET `number` = 5 WHERE `id` = 1;

# After:
UPDATE `widgets` SET `number` = `number` + 5 WHERE `id` = 1;

Install

sudo gem sources -a http://gems.github.com # unless you already have it
sudo gem install arya-ar-deltas

Use

class Widget < ActiveRecord::Base
  delta_attributes :number_one, :number_two
end

Notes

  • Only works with numeric columns
  • Tested in with ActiveRecord 2.3.3 and 3.0.0pre
  • You don't have to use += or -=, it uses the changes hash to figure out the difference to apply
  • If you're using write-through caching, be careful about running into inconsistencies