aboutsummaryrefslogblamecommitdiffstats
path: root/activemodel/examples/validations.rb
blob: 3f8311ff96cce6056bc4aaec7ea462d7aae80591 (plain) (tree)
1
2
3
4
5
6
7
8
9
                     

            
                                 
                                  
 
                             
 
                     
 


                                 
 


                     
 


                 






                                     
                
require 'activemodel'

class Person
  include ActiveModel::Conversion
  include ActiveModel::Validations

  validates_presence_of :name

  attr_accessor :name

  def initialize(attributes = {})
    @name = attributes[:name]
  end

  def persist
    @persisted = true
  end

  def new_record?
    @persisted
  end
end

person1 = Person.new
p person1.valid?
person1.errors

person2 = Person.new(:name => "matz")
p person2.valid?