Getting A Users Change History Using Audited Gem

Posted by Weston Ganger

Using the audited gem they didnt clearly define how to get a list of all of the changes / history the user has made. I wrote a method to do this


class User < ActiveRecord::Base
  #include Audited ###if your not using Rails or trying this on a different class that is not hooked up to audited yet

  def history
    Audited.audit_class.where(user_id: self.id).order(created_at: :desc)
  end
end

Then it can be used like so:


@user.history

Make that into an each and generate the history page for your user!

Article Topic:Software Development - Rails

Date:October 17, 2015

Recommended Posts