Skip to content

Instantly share code, notes, and snippets.

@nicedawg
Created March 28, 2020 21:06
Show Gist options
  • Save nicedawg/c28d5a14e7182d606ef5bf01b68779ee to your computer and use it in GitHub Desktop.
Save nicedawg/c28d5a14e7182d606ef5bf01b68779ee to your computer and use it in GitHub Desktop.
[Beginning Rails 6] Listing 13-5. sending "comment added" mailer asynchronously
class Comment < ApplicationRecord
belongs_to :article
validates :name, :email, :body, presence: true
validate :article_should_be_published
after_create :email_article_author
def article_should_be_published
errors.add(:article_id, 'is not published yet') if article && !article.published?
end
def email_article_author
NotifierMailer.comment_added(self).deliver_later
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment