What's the difference between "validate" and "validates"?

Ruby on-RailsValidation

Ruby on-Rails Problem Overview


I added a validation to an objects using:

validate :field, presence: true

I found they do not give error messages. I changed them to validates and got the error messages. This ticket is relevant.

I tried to add a custom validation with validates and got an error:

You need to supply at least one validation

I changed it to validate, and everything went along as expected.

My understanding is to use validates with normal validations, and validate with custom ones. Is that right? Are there any other pieces I should know about? Is there any way to have the first problem fail loudly and not just validate everything?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucy BainView Question on Stackoverflow
Solution 1 - Ruby on-Railsrmagnum2002View Answer on Stackoverflow
Solution 2 - Ruby on-RailsVirenView Answer on Stackoverflow