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?)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With