Skip to content
Create independent sets of validations with the full power of ActiveRecord's validation macros
Latest commit 6e9c4e8 Jun 18, 2015 @gtd Bump to 0.5.2

Validation Scopes Build Status

This gem adds a simple class method validation_scope to ActiveRecord. This generates a new collection of ActiveRecord::Errors that can be manipulated independently of the standard errors, valid? and save methods. The full power of ActiveRecord validations are preserved in these distinct error collections, including all the macros.

For example, in addition to standard errors that prevent an object from being saved to the database, you may want a second collection of warnings that you display to the user or otherwise shape the control flow:

class Film < ActiveRecord::Base
  validates_presence_of :title # Standard errors

  validation_scope :warnings do |s|
    s.validate :ensure_title_is_capitalized
    s.validate { |r| r.warnings.add_to_base("Inline warning") }

  def ensure_title_is_capitalized
    warnings.add(:title, "should be capitalized") unless title =~ %r{\A[A-Z]}

The generated scope produces 3 helper methods based on the symbol passed to the validation_scope method. Continuing the previous example:

film = => 'lowercase title')
=> true

film.no_warnings?   # analagous to valid?
=> false

film.has_warnings?  # analagous to invalid?
=> true

film.warnings       # analagous to film.errors
=> #<ActiveRecord::Errors>

=> ["Title should be capitalized", "Inline warning"]

=> []

=> [:warnings]
=> true

One rough edge at the moment is when you want to use the builtin error_messages_for helper in your views. That helper does not accept an ActiveRecord::Errors object directly. Instead you need to pass it the proxy object that ValidationScopes creates to encapsulate the generated error set:

error_messages_for :object => film.validation_scope_proxy_for_warnings


The current version should work for Rails >= 3.0 and Ruby >= 1.9.2.

For Rails 3 and Ruby 1.8.x use version 0.4.x, however beware there is a memory leak in this version as described here

For Rails 2 see the 0.3.x version of the gem which is maintained on the rails2 branch


The usual:

gem install validation_scopes

In your Gemfile:

gem 'validation_scopes'

Or without Bundler:

require 'validation_scopes'

Don't use private methods

Because the any validation method supplied as a symbol (eg. validate :verify_something) is actually running in the context of a delegate class, private methods won't work as they would in standard validations.


  • In Rails 3 validations are no longer coupled to ActiveRecord. Although the current version of the gem uses ActiveModel, it hasn't been tested against arbitrary objects.


Copyright (c) 2010-2015 Gabe da Silveira. See LICENSE for details.

Something went wrong with that request. Please try again.