By introduction, Justin Weiss is a rather prolific blogger, journaler, and writer about Rails. He has a book that’s coming out, Practicing Rails that is the book I wish I had written, and I am thoroughly indebted to Justin for writing. The book is exactly the way I go about learning how programming concepts, language features, framework features, and all that stuff work.

In this article, Justin covers Rails’s validation contexts, a poorly-documented (to date) feature that allows different validation tests to be run during model operations. The article references another article, http://blog.arkency.com/2014/04/mastering-rails-validations-contexts/, which goes even further into the topic.

In a nutshell, or, the TL;DR

You can read (and you should read) those other articles for more details. I’m merely going to capture the bottom line here.

In the model, you define validation contexts using the on: keyword:

class Post < ActiveRecord::Base
  # ...
  validates_precense_of :body, on: :publish
  # ...
end

To use validation contexts, you pass in the context to the .valid? method of the model:

@post.valid?(:publish)

The examples indicate this is can be used in the PostsController for example, but Justin’s book goes even a bit further in explaining how they’re really useful in testing, and in the Rails console (i.e. REPL driven development).