Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'active_model'
class Person
include ActiveModel::Validations
attr_accessor :name, :age
def initialize(name: nil, age: nil)
@name = name
@age = age
end
validates :name, :age , presence: true
validates :age, numericality: { greater_than: 0 }
end
puts 'Empty Person'
person = Person.new
p person.valid?
p person.errors.messages
puts ' with name'
person = Person.new name: 'Bruce Willis'
p person.name
p person.valid?
p person.errors.messages
puts 'With age equal 0'
person = Person.new name: 'Bruce Willis', age: 0
p person.age
p person.valid?
p person.errors.messages
puts 'With age greater than 0'
person = Person.new name: 'Bruce Willis', age: 56
p person.name
p person.age
p person.valid?
p person.errors.messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.