In Rails 4.1 and later (see section 7.2 in the release notes) ActiveModel validations can be defined with an
on: :some_context modifier. Which will trigger the validation check only when the modifier is passed in to
save. For example:
class Person include ActiveModel::Validations attr_accessor :name validates_presence_of :name, on: :name_required end person = Person.new person.name = '' person.valid? # => true person.valid?(:name_required) # => false
Person < ActiveRecord::Base you can also pass the
context into save like so:
person.save #=> true person.save(context: :name_required) #=> false
More than likely if you see this it's a smell and your model is doing too much. But if you must add a layer of complication to your model validations, Rails has it built in.