Reputation: 46
What is validates
in ruby, not in rails? Is it a class variable, or some sort of code that doesn't need to be in def initialize
method for the class Person
?
class Person < ActiveRecord::Base
validates :name, presence: true
end
Upvotes: 1
Views: 102
Reputation: 168199
It is a class method. Its receiver is the class Person
. Allowing the self
receiver to be omitted, parentheses to be omitted, the arrow in a hash to be omitted when the key is a symbol, and braces around the hash literal to be omitted in the final-argument position make such DSL possible. If you fully write the method call in the ordinary way, it would look like:
Person.validates(:name, {:presence => true})
Upvotes: 4