Guest User

Untitled

a guest
Jul 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. class Category < ActiveRecord::Base
  2. validates_presence_of :title
  3.  
  4. private
  5.  
  6. def validate
  7. errors.add(:description, "is too short") if (description.size < 200)
  8. end
  9. end
  10.  
  11. class Category < ActiveRecord::Base
  12. validates_presence_of :title
  13. validate :description_length
  14.  
  15. def description_length
  16. errors.add(:description, "is too short") if (description.size < 200)
  17. end
  18. end
  19.  
  20. class TitleValidator < ActiveModel::EachValidator
  21. Titles = ['Mr.', 'Mrs.', 'Dr.']
  22. def validate_each(record, attribute, value)
  23. unless Titles.include?(value)
  24. record.errors[attribute] << 'must be a valid title'
  25. end
  26. end
  27. end
  28.  
  29. class Person
  30. include ActiveModel::Validations
  31. attr_accessor :title
  32. validates :title, :presence => true, :title => true
  33. end
  34.  
  35. # Or for Active Record
  36.  
  37. class Person < ActiveRecord::Base
  38. validates :title, :presence => true, :title => true
  39. end
Add Comment
Please, Sign In to add comment