Last active
August 29, 2015 14:17
-
-
Save acrookston/13bd4403d87e7ce336e8 to your computer and use it in GitHub Desktop.
Rails update_attributes without updating the updated_at field
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 Example < ActiveRecord::Base | |
include WithoutTimestamps | |
def update_something | |
update_without_timestamps something: "is changed" | |
end | |
def alternatively | |
self.something = "is changed" | |
update_without_timestamps | |
end | |
end |
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
# models/concerns/without_timestamps.rb | |
module WithoutTimestamps | |
def update_without_timestamps(attributes=nil) | |
class << self | |
def record_timestamps; false; end | |
end | |
self.attributes = attributes if attributes | |
success = save | |
class << self | |
remove_method :record_timestamps | |
end | |
success | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment