Last active
August 29, 2015 13:57
-
-
Save scottwater/9762126 to your computer and use it in GitHub Desktop.
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
before_update :audit | |
def audit | |
if self.changed? | |
# find columns we want to audit | |
audit_columns = UserAudit.column_names.reject{|c| %w{id created_at updated_at}.include?(c)} | |
# add the user_id | |
data = self.attributes.dup.merge('user_id' => self.id) | |
# copy the pre-modified values | |
self.changes.each {|att, a| data[att] = a[0]} | |
# only keep the columns we columns we care about | |
data.keep_if{|c, v| audit_columns.include?(c)} | |
# save data | |
UserAudit.create!(data) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment