Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created March 14, 2009 10:45
Show Gist options
  • Select an option

  • Save methodmissing/79042 to your computer and use it in GitHub Desktop.

Select an option

Save methodmissing/79042 to your computer and use it in GitHub Desktop.
# Reset id and @new_record if the transaction rolls back.
def rollback_active_record_state!
id_present = has_attribute?(self.class.primary_key)
previous_id = id
previous_new_record = new_record?
yield
rescue Exception
@new_record = previous_new_record
if id_present
self.id = previous_id
else
@attributes.delete(self.class.primary_key)
@attributes_cache.delete(self.class.primary_key)
end
raise
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment