Reputation: 12759
What is the best/cleanest/easiest way to maintain the edit history of records in Rails?
I'm looking for logging - who made the edits and when and the ability to rollback to earlier versions of records.
My guess is that you would use ActiveRecord callbacks on updates or deletes and instead of updating/deleting records you would create a new one and have some sort of identifier to keep the same "record" associated, maybe a field to distinguish which record is current, and a version field.
I vaguely recall seeing some plugins but I can't seem to find them at the moment.r
(Is there a term for this that I don't know?)
Upvotes: 6
Views: 4356
Reputation: 2549
The answers here are good, but need some updates.
acts_as_audited has been renamed to audited, see https://github.com/collectiveidea/audited. I've used it with great success.
paper_trail may also be of interest. http://railscasts.com/episodes/255-undo-with-paper-trail restoring versions is a little awkward and undocumented in audited/acts_as_audited. paper_trail has clearer examples for this aspect.
Upvotes: 8
Reputation: 15644
acts_as_audited wins hands down. You can even use acts_as_versioned with it. The plugin-page explains everything. Go through the discussion comments below the post on the page. Developers have posted issues and have obtained positive responses from the author and others.
I have used this plugin in many apps and I find it very very useful. Highly recommended.
Here is a preview from the plug-in page:
acts_as_audited is an Active Record plugin that logs all modifications to your models in an audits table. It uses a polymorphic association to store an audit record for any of the model objects that you wish to have audited. The audit log stores the model that the change was on, the “action” (create, update, destroy), a serialzied hash of the changes, and optionally the user that performed the action.
Upvotes: 12
Reputation: 6638
acts_as_versioned should do what you're looking for - http://wiki.rubyonrails.org/rails/pages/ActsAsVersioned
Upvotes: 3