Skip to content

Instantly share code, notes, and snippets.

@scottwater
Last active August 29, 2015 13:57
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 scottwater/9762126 to your computer and use it in GitHub Desktop.
Save scottwater/9762126 to your computer and use it in GitHub Desktop.
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