What's the difference between “validate” and “validates”?

前端 未结 2 836
天涯浪人
天涯浪人 2020-12-08 12:53

I added a validation to an objects using:

validate :field, presence: true

I found they do not give error messages. I changed them to

相关标签:
2条回答
  • 2020-12-08 13:26

    I believe the :validate declaration is used for custom validation where as :validates is used for generic validation like presence, uniqueness etc on a field

    The validate method looks for a method with the parameter's name, i.e. if you do validate :field it will look for

    def field 
    
    end
    

    on your object. Since Rails defines an attr_accessor for every database field the validate :field would call the field's reader method.

    If the validation function returns true or if there is an error object, i.e. object.errors is not empty, then the object is considered valid?

    Hence the reason in ligthouse issue, they complain that validate silently bypasses the validation :)

    Hope this make sense

    0 讨论(0)
  • 2020-12-08 13:33

    validates This method is a shortcut to all default validators and any custom validator classes ending in ‘Validator’. Note that Rails default validators can be overridden inside specific classes by creating custom validator classes in their place such as PresenceValidator.

    validates :title, :body, :presence => true
    

    validate, 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.

    validate :must_be_friends
    
      def must_be_friends
        errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
      end
    
    0 讨论(0)
提交回复
热议问题