Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created March 4, 2010 22:26
Show Gist options
  • Select an option

  • Save xaviershay/322188 to your computer and use it in GitHub Desktop.

Select an option

Save xaviershay/322188 to your computer and use it in GitHub Desktop.
# DM Extensions
# Use for roll your own optimistic locking, without using a dedicated lock_version column
def update_with_conditions(attributes, conditions)
properties = attributes.inject({}) do |a, (key, value)|
a.update(property_by_name(key) => value)
end
updated_count = repository.adapter.update(properties, all(conditions))
raise StaleObjectError if updated_count != 1
end
def property_by_name(name)
properties.detect {|x| x.name == name }
end
# Code
IndexInstrument.update_with_conditions({:name => 'NEW'},
:id => existing.id,
:name => 'OLD')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment