Mohan Sandesh
Mohan Sandesh

Reputation: 46

What is this property called in a ruby class

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

Answers (2)

sawa
sawa

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

mccalljt
mccalljt

Reputation: 796

Validates is rails specific.

Here it is in the rails api docs

Here it is in GitHub

You can write your own validators without rails by creating a method that runs on initialization and then throws an exception if it a criteria is not met.

Upvotes: 0

Related Questions