Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This gist is written to highlight an issue which is coming up with the audited gem by collectiveidea
The audited gem is a powerful gem that helps us keeping the log of the changes that are made to the models.
This gem does its job excellently but there are some issues which I have discovered when I was reading this gem..
Suppose I have a model named Customer as shown below
class Customer < ActiveRecord::Base
attr_accessible :first_name
audited :comment_required => true
The gem performs as it is expected with no exceptions
But, now lets come to the problem part. The main problem which I found out was with the :on option that you can supply.
The :on option is basically a directive that is set on the audited gem which would audit the changes only when that particular operation is performed. The :on option along with the :comment_required would mean that you want to have your comments enabled for those actions which are specified in the :on option. Lets throw some more light on it...
class Customer < ActiveRecord::Base
attr_accessible :first_name
audited :comment_required => true, :on => [:update]
c = Customer.first
c.update_attributes(:first_name => "Updated Name", :audit_comment => "Audit Comment") #=> true
c.update_attributes(:first_name => "Updated Name") #=> false
The above three lines work as expected but when we create or destroy a customer, the real problem starts...
c = Customer.create(:first_name => "Customer") #=> Rollback
c.errors.full_messages #=> ["Audit comment can't be blank"]
c.destroy #=> false
c.errors.full_messages #=> ["Audit comment can't be blank"]
In the model definition, we have specified that we want to have the auditing only for the update action and thus, there should not be any traces left of this gem in the other actions namely, destroy and create..
Special Mentions and Thanks to Akshay (github: who helped me in the discussions and solution to the problem...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment