Using before_update Callback
In this guide we'll implement a before_update callback so that our database confirmation records will automatically update properly.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

We are going to address each change now. In this video, we'll update the end date when an audit log is confirmed using a callback called before_update. We are not going to use a test case here because we'll only be testing the callback functionality of rails, which makes no sense.

So, open audit_log.rb and create the callback, like this:

# app/models/audit_log.rb

before_update :set_end_date, if: :confirmed?

private
  def set_end_date
    self.end_date = Date.today
  end

In our callback, we are calling a method called set_end_date only if the log has a status of "confirmed." In the method, we are simply setting the value of end_date to today.

Before testing, let's clear the database and run the seeds.rb file. Open the browser, log in as an employee and confirm a time entry. Now, log in as an admin, navigate to Audit Log tab and check the status.

Resources