Validações no Ruby on Rails

11,934 views

Published on

Breve apresentação sobre algumas validações do Rails no Model

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
11,934
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Validações no Ruby on Rails

  1. 1. Validações! http://edgeguides.rubyonrails.org/ active_record_validations.html
  2. 2. Validações Rails • Configurações no modelo • Garantem que somente objetos válidos sejam persistidos • Métodos que disparam • create • save • update • update_attributes
  3. 3. Obrigatório class Person < ActiveRecord::Base validates :name, presence: true validates_presence_of :idade end
  4. 4. Tamanho class Person < ActiveRecord::Base validates :name, length: { minimum: 2 } validates :bio, length: { maximum: 500 } validates :password, length: { in: 6..20 } validates :registration_number, length: { is: 6 } end
  5. 5. Tamanho class Essay < ActiveRecord::Base validates :content, length: { minimum: 300, maximum: 400, too_short: "minimo %{count} caracteres", too_long: "maximo %{count} caracteres" } end
  6. 6. Numéricos class Player < ActiveRecord::Base validates :points, numericality: true validates :games_played, numericality: { only_integer: true } end
  7. 7. Numéricos :greater_than :equal_to :greater_than_or_equal_to :odd :less_than :even :less_than_or_equal_to ! !
  8. 8. Unicidade class Account < ActiveRecord::Base validates :email, uniqueness: true end
  9. 9. Unicidade class Holiday < ActiveRecord::Base validates :name, uniqueness: { scope: :year, message: "should happen once per year" } end
  10. 10. Unicidade class Person < ActiveRecord::Base validates :name, uniqueness: { case_sensitive: false } end

×