validate
 
  - 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (11)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (38)
- 4.1.8 (-1)
- 4.2.1 (2)
- 4.2.7 (5)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (4)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
validate(*args, &block)
  public
  Adds a validation method or block to the class. This is useful when overriding the validate instance method becomes too unwieldy and you’re looking for more descriptive declaration of your validations.
This can be done with a symbol pointing to a method:
class Comment include ActiveModel::Validations validate :must_be_friends def must_be_friends errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee) end end
With a block which is passed with the current record to be validated:
class Comment include ActiveModel::Validations validate do |comment| comment.must_be_friends end def must_be_friends errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee) end end
Or with a block where self points to the current record to be validated:
class Comment include ActiveModel::Validations validate do errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee) end end
Note that the return value of validation methods is not relevant. It’s not possible to halt the validate callback chain.
Options:
- 
:on - Specifies the contexts where this validation is active. Runs in all validation contexts by default nil. You can pass a symbol or an array of symbols. (e.g. on: :create or on: :custom_validation_context or on: [:create, :custom_validation_context]) 
- 
:if - Specifies a method, proc, or string to call to determine if the validation should occur (e.g. if: :allow_validation, or if: Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value. 
- 
:unless - Specifies a method, proc, or string to call to determine if the validation should not occur (e.g. unless: :skip_validation, or unless: Proc.new { |user| user.signup_step <= 2 }). The method, proc, or string should return or evaluate to a true or false value. 
NOTE: Calling validate multiple times on the same method will overwrite previous definitions.
 nhance -  
    June 28, 2011
 nhance -  
    June 28, 2011 
    
  Typo in example above
You aren’t mistaken. That is an error in the example with the block above. There’s an extra ‘(’ character.
 ggarnier -  
    May 28, 2012
 ggarnier -  
    May 28, 2012 
    
  Accepted parameters for validate
Validate method also accepts :on and :if parameters. The default value for :on is :save, the other accepted values are :create and :update
class Comment include ActiveModel::Validations validate :must_be_friends, :on => :create, :if => Proc.new {|comment| some_condition} def must_be_friends errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee) end end

 RSpec
RSpec Ruby on Rails
Ruby on Rails Ruby
Ruby 
   
   
   
    
 
   
   
   = protected
 = protected
   
    