Skip to content

Instantly share code, notes, and snippets.

@bricemaurin
Created October 28, 2014 15:59
Show Gist options
  • Save bricemaurin/f193ed64ba67b2d0ef68 to your computer and use it in GitHub Desktop.
Save bricemaurin/f193ed64ba67b2d0ef68 to your computer and use it in GitHub Desktop.
AR-02-Validations
class Post < ActiveRecord::Base
default_scope order('votes DESC')
belongs_to :user
validates :name, :url, :user, presence: true
validates :name, length: { minimum: 5 }
validates :name.downcase, uniqueness: { case_sensitive: false }
validates :url, format: { with: /\Ahttps?\:\/\/(?:www\.|)?\w+\.\w{2,3}(?:\/\w+)?/, message: "invalid url" }
end
class User < ActiveRecord::Base
has_many :posts
before_validation :stripspaces
validates :username, :email, presence: true
validates :email, format: { with: /\A\w+@\w+\.\w{2,3}\z/, message: "invalid email" }
validates :username, uniqueness: true
end
def stripspaces
self.email = email.lstrip.rstrip unless email.nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment