Skip to content

Instantly share code, notes, and snippets.

@ahmetabdi
Created April 2, 2014 11:05
Show Gist options
  • Save ahmetabdi/9932034 to your computer and use it in GitHub Desktop.
Save ahmetabdi/9932034 to your computer and use it in GitHub Desktop.
Validating Multiple Fields in Rails
validate :unique_organisation_entry
def unique_organisation_entry
matched_entry = Organisation.where(['LOWER(name) = LOWER(?) AND LOWER(company_no) = LOWER(?) AND LOWER(postcode) = LOWER(?)', self.name, self.company_no, self.postcode]).first
errors.add(:base, 'I know you think you\'re clever, but someone already submitted that one.') if matched_entry && (matched_entry.id != self.id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment