class User < ActiveRecord::Base
  # keep the default scope first (if any)
  default_scope { where(active: true) }
  # constants come up next
  COLORS = %w(red green blue)
  # afterwards we put attr related macros
  attr_accessor :formatted_date_of_birth
  attr_accessible :login, :first_name, :last_name, :email, :password
  # Rails4+ enums after attr macros, prefer the hash syntax
  enum gender: { female: 0, male: 1 }
  # followed by association macros
  belongs_to :country
  has_many :authentications, dependent: :destroy
  # and validation macros
  validates :email, :username, presence: true
  validates :username, uniqueness: { case_sensitive: false }
  validates :username, format: { with: /\A[A-Za-z][A-Za-z0-9._-]{2,19}\z/ }
  validates :password, format: { with: /\A\S{8,128}\z/, allow_nil: true }
  # next we have callbacks
  before_save :cook
  before_save :update_username_lower
  # other macros (like devise's) should be placed after the callbacks
  ...
end
          Created
          August 22, 2017 17:56 
        
      - 
      
- 
        Save BrianSigafoos/f0fe5e6417b0acf70ffe1bc6b6498796 to your computer and use it in GitHub Desktop. 
    Rails model layout and validations
  
        
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment