Repository is archived
No commit activity in last 3 years
No release in over 3 years
ActiveRecord#update, but only when needed.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 4.2.0
 Project Readme

update_if_changed Circle CI Code Climate

Ruby on Rails' ActiveRecord#update, but only when needed.

foo = Foo.create(bar: "this is a test")

ActiveRecord#update runs transaction callbacks, even when nothing was updated:

foo.update(bar: "this is a test")
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
# => true

ActiveRecord#update_if_changed avoid calling ActiveRecord#update when possible:

foo.update_if_changed(bar: "this is a test")
# => nil

Other than that, it just calls ActiveRecord#update like you would expect:

foo.update_if_changed(bar: "it worked!")
   (0.1ms)  begin transaction
  SQL (0.4ms)  UPDATE "foos" SET "bar" = ?, "updated_at" = ? WHERE "foos"."id" = ?  [["bar", "it worked!"], ["updated_at", "2015-02-03 18:35:18.887834"], ["id", 1]]
   (2.1ms)  commit transaction
# => true

Time to go crazy:

gem 'update_if_changed', '~> 0.0.1'