Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to maintain a record's edit history with Rails and ActiveRecord

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?)

like image 201
srboisvert Avatar asked Jan 28 '09 13:01

srboisvert


People also ask

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is application record in Rails?

Now ApplicationRecord will be a single point of entry for all the customizations and extensions needed for an application, instead of monkey patching ActiveRecord::Base. Say I want to add some extra functionality to Active Record. This is what I would do in Rails 4.2.

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is ActiveRecord relation?

The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.


1 Answers

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.

like image 194
Chirantan Avatar answered Oct 11 '22 10:10

Chirantan