validações no ruby on rails

Download Validações no Ruby on Rails

Post on 19-Jan-2015

2.616 views

Category:

Technology

7 download

Embed Size (px)

DESCRIPTION

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

TRANSCRIPT

  • 1. Validaes!http://edgeguides.rubyonrails.org/ active_record_validations.html

2. Validaes Rails Conguraes no modeloGarantem que somente objetos vlidos sejam persistidosMtodos que disparam createsaveupdateupdate_attributes 3. Obrigatrio class Person < ActiveRecord::Base validates :name, presence: true validates_presence_of :idade end 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. Tamanho class Essay < ActiveRecord::Base validates :content, length: { minimum: 300, maximum: 400, too_short: "minimo %{count} caracteres", too_long: "maximo %{count} caracteres" } end 6. Numricos class Player < ActiveRecord::Base validates :points, numericality: true validates :games_played, numericality: { only_integer: true } end 7. Numricos :greater_than:equal_to:greater_than_or_equal_to:odd:less_than:even:less_than_or_equal_to ! ! 8. Unicidade class Account < ActiveRecord::Base validates :email, uniqueness: true end 9. Unicidade class Holiday < ActiveRecord::Base validates :name, uniqueness: { scope: :year, message: "should happen once per year" } end 10. Unicidade class Person < ActiveRecord::Base validates :name, uniqueness: { case_sensitive: false } end