Created
July 19, 2012 12:38
-
-
Save romansklenar/3143559 to your computer and use it in GitHub Desktop.
Rails ActiveRecord Model protected against overwrite by another user updating same instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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