class Notification < ApplicationRecord belongs_to :member scope :scheduled_for_today, ->() { where(status: 'scheduled', to_be_sent_on: Date.today) } def self.send_scheduled_for_today scheduled_for_today.each do |n| n.deliver! end end def deliver! # actually send the email. NotificationMailer.with(notification: self).send(template).deliver_now! update(status: 'sent', sent_at: Time.current) rescue # TODO: do something about failures end end