class Virtual < ApplicationRecord validate :domain_name_exists validates :email, presence: true validates :destination, presence: true validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "email must be valid"} validates :destination, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "destination must be valid"} validates :destination, uniqueness: {scope: :email, message: "a redirection pair must be unique"} def domain_name_exists if email.present? if !email.index("@").nil? split_email = email.split("@") if !Domain.exists?(domain: split_email[1]) errors.add(:email, "domain must be looked after by this server") end end end end end