Enable Logging for AR Objects - A Tutorial

The enable_logging plugin will extend your active_record models with an audit table. Auditing is the monitoring and recording of selected user database actions. Auditing can be used to:

  • log changes to (important) model objects
  • investigate suspicious activity
  • monitor and gather data about specific database activities

This tutorial will extend the Application build in Part II of [Agile Web Development with Rails] (2nd Edition, Page 59 et sqq.). For a quick introduction visit QuickInstall.

Extending the Product Model to log changes

Assuming you got the AdminController? (page 67) up and running and you want to log all changes to the product, the first thing you need to do is to install the plugin:

 bash$ ./script/plugin install http://svn.omdb-beta.org/plugins/enbale_logging

to activate the logging, you need to create the audit-table. This can be automatically genereted via

 bash$ ./script/generate enable_logging_migration add_log_entries_table
 bash$ rake db:migrate

The basic installation is done, all changes to your models will be logged, as soon as you selected the models you want to log changes for. You started your AdminController? with a basic scaffolding. We want to log all changes to the product objects, so you can see the history of the product. Your controller currently look like this (see page 67).

  class AdminController < ApplicationController
    scaffold :product
  end

To activate the logging, you need to extend the Controller with one more row, it should look like this:

  class AdminController < ApplicationController
    scaffold :product
    log_changes_for :product
  end

The scaffold view will automatically be extended with a history view. You can access the history view via the list view:

Scaffold - now with histroy view

Scaffold - history view

Attachments