What are we going to do?

  • We're going to be looking at ActiveRecord::Events
  • I'm going to show you how to setup & use this gem
  • You're going to see how much of a time saver this gem is!

ActiveRecord::Events

Manage timestamps in ActiveRecord models

What Are We Trying To Solve?

What Are We Trying To Solve?

class Task < ApplicationRecord
  def completed?
    completed_at.present?
  end

  def complete!
    touch(:completed_at)
  end

  def started?
    started_at.present?
  end
end

What Are We Trying To Solve?

task = Task.last
task.started?

# I now expect this method to be present
task.start!

# NoMethod error? What!

What Are We Trying To Solve?

class Task < ApplicationRecord
  has_event :complete

  # We now have the instance methods:
  # Task#completed?
  # Task#complete!
  #
  # Along with the class method:
  # Task::complete_all
  #
  # And the scopes:
  # Task::completed
  # Task::not_completed
end

Installing

Add to Gemfile:

$ bundle add active_record-events

Generate the Migration

$ rails generate active_record:event task complete

Resources

Questions?

MikeRogers.io
@MikeRogers0 on Twitter

It's a good pattern to store boolean fields as timestamps. Not only do you know if something is true, you know when it was set to be true. Tweet by Jake Wright Backend engineer at monzo

However this can lead to code where we repeat code which does the same kind of things.

It may also lead to a scenario where some methods are defined but others aren't.

The ActiveRecord::Events it'll give us a nice helper, that'll generate those handy methods consistently