aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/examples/validations.rb
blob: b8e74acd5e0710690c47baaefd7e5a5caf22ab65 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require File.expand_path('../../../load_paths', __FILE__)
require 'active_model'

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

  validates :name, presence: true

  attr_accessor :name

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

  def persist
    @persisted = true
  end

  def persisted?
    @persisted
  end
end

person1 = Person.new
p person1.valid? # => false
p person1.errors.messages # => {:name=>["can't be blank"]}

person2 = Person.new(name: 'matz')
p person2.valid? # => true