Integrating Custom Callback for Audit Status Workflow
Learn how to use callbacks for automating the audit status workflow process.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So far, we have designed and built the homepage for both employees and admin users. Now, Let's say an employee enters an overtime request for a particular week. After he saves his overtime request, the homepage should not have a message that's asking him to confirm that he did not perform overtime for that week because he just confirmed it by entering a request. That's counter-intuitive, and that's what we are going to fix in this video.

To do this, we are going to use the after_save callback in our post.rb file. We are going to call a private method called update_audit_log in this callback, and then, we are going to define this method. Here, we have to find the audit log that is associated to the current user and has a start date that is between today and the last seven days, and then confirm it.

# app/models/post.rb

private

  def update_audit_log
    audit_log = AuditLog.where(user_id: self.user_id, start_date: (self.date - 7.days..self.date)).last
    audit_log.confirmed!
  end

Resources