Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Validações!

http://edgeguides.rubyonrails.org/
active_record_validations.html
Validações Rails
•

Configurações no modelo

•

Garantem que somente objetos válidos
sejam persistidos

•

Métodos que disp...
Obrigatório
class Person < ActiveRecord::Base
validates :name, presence: true
validates_presence_of :idade
end
Tamanho
class Person < ActiveRecord::Base
validates :name, length: { minimum: 2 }
validates :bio, length: { maximum: 500 }...
Tamanho
class Essay < ActiveRecord::Base
validates :content, length: {
minimum: 300,
maximum: 400,
too_short: "minimo %{co...
Numéricos
class Player < ActiveRecord::Base
validates :points, numericality: true
validates :games_played, numericality: {...
Numéricos
:greater_than

:equal_to

:greater_than_or_equal_to

:odd

:less_than

:even

:less_than_or_equal_to
!
!
Unicidade
class Account < ActiveRecord::Base
validates :email, uniqueness: true
end
Unicidade
class Holiday < ActiveRecord::Base
validates :name, uniqueness: { scope: :year,
message: "should happen once per...
Unicidade
class Person < ActiveRecord::Base
validates :name, uniqueness: { case_sensitive: false }
end
Upcoming SlideShare
Loading in …5
×

Validações no Ruby on Rails

15,095 views

Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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

×