Created
October 28, 2014 15:59
-
-
Save bricemaurin/f193ed64ba67b2d0ef68 to your computer and use it in GitHub Desktop.
AR-02-Validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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