1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/examples/validations.rb

30 lines
493 B
Ruby
Raw Normal View History

2009-09-14 15:54:43 -04:00
require 'active_model'
class Person
2009-07-21 01:56:27 -04:00
include ActiveModel::Conversion
include ActiveModel::Validations
2009-07-21 01:56:27 -04:00
validates :name, presence: true
2009-07-21 01:56:27 -04:00
attr_accessor :name
2009-07-21 01:56:27 -04:00
def initialize(attributes = {})
@name = attributes[:name]
end
2009-07-21 01:56:27 -04:00
def persist
@persisted = true
end
2009-07-21 01:56:27 -04:00
def persisted?
@persisted
end
end
person1 = Person.new
p person1.valid? # => false
p person1.errors.messages # => {:name=>["can't be blank"]}
2013-07-04 12:25:50 -04:00
person2 = Person.new(name: 'matz')
p person2.valid? # => true