Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.