Skip to content

Instantly share code, notes, and snippets.

@romansklenar
Created July 19, 2012 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romansklenar/3143559 to your computer and use it in GitHub Desktop.
Save romansklenar/3143559 to your computer and use it in GitHub Desktop.
Rails ActiveRecord Model protected against overwrite by another user updating same instance
class Foo < ActiveRecord::Base
attr_accessor :timestamp_control
after_initialize { self.timestamp_control ||= Time.zone.now }
before_validation :overwrite_check
def overwrite_check
errors[:base] << I18n.t('errors.messages.overwritten') if updated_at > timestamp_control
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment