I am trying to implement an audit trail into my application, but because of some requirements I am unable to use any existing gems or plugins.
I would like to divert any normal attempt to update a model to a custom method that saves all the updates in another separate Table (called Updates).
The application then uses the update table to actually perform the update.
Right now I have overloaded create_or_update to get the first part of the functionality
def create_or_update
raise ReadOnlyRecord if readonly?
result = new_record? ? create : create_updates
result != false
end
class Update < ActiveRecord::Base
belongs_to :updatable, :polymorphic => true
after_create :update_model
private
def update_model
self.updatable.update_attribute self.attribute, self.new_value #infinite loop
end
end
The issue now is that this causes an infinite loop when the Update Model tries to actually perform the update.
I have been looking through the rails core source to find the best place to bypass the functionality of the first feature. I would like these updates to be performed inside of the transaction but I am not sure where exactly that begins or ends in the active record stack. I also do not want to start hacking away at active resource.
Any suggestions would be greatly appreciated.