So i have to 1) not save changes but instead 2) save the audit with these changes. The second part is achieved by send_for_audit
method, but if i return false in it the new Audit instance is not created as well as changes to the Article instance are not saved (there is the simplified code).
class Article < ActiveRecord::Base
before_save :send_for_audit, if: :changed?
has_many :audits, as: :auditable
private
def send_for_audit
audits.destroy_all
Audit.create!(auditable: self, data: changes)
end
end
class Audit < ActiveRecord::Base
attr_accessible :auditable, :auditable_id, :auiditable_type, :data
store :data
belongs_to :auditable, polymorphic: true
def accept
object_class = self.auditable_type.constantize
object = object_class.find_by_id(auditable_id)
data.each do |attr, values|
object.update_column(attr.to_sym, values.last)
end
self.destroy
end
end
I've tried to add an additional before_save callback thinking that the order in which they are triggered will do the trick:
before_save :send_for_audit, if: :changed?
before_save :revert_changes
def send_for_audit
audits.destroy_all
Audit.create!(auditable: self, data: changes)
@need_to_revert = true
end
def revert_changes
if @need_to_revert
@need_to_revert = nil
false
end
end
but anyway i got no Audit instance.. Any thoughts how i could achieve the desired result?