Skip to content

Instantly share code, notes, and snippets.

@nicedawg
Last active February 25, 2020 01:36
Show Gist options
  • Save nicedawg/03868d69e4b318cfcd73d87fe495dd23 to your computer and use it in GitHub Desktop.
Save nicedawg/03868d69e4b318cfcd73d87fe495dd23 to your computer and use it in GitHub Desktop.
[Beginning Rails 6] Listing 6-32. The email_article_author Method Specified as an after_create Callback in app/models/comment.rb
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
puts "We will notify #{article.user.email} in Chapter 12" if article.user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment