Skip to content

Instantly share code, notes, and snippets.

@CodeMonkeyKevin
Created February 8, 2011 22:37
Show Gist options
  • Save CodeMonkeyKevin/817432 to your computer and use it in GitHub Desktop.
Save CodeMonkeyKevin/817432 to your computer and use it in GitHub Desktop.
CouchPotato unique validation for couchdb
class Account
include CouchPotato::Persistence
property :name
view :by_name, :key => :name
validates :name, :presence => true, :couch_unique => true
before_validation :downcase_name
private
def downcase_name
self.name = name.to_s.downcase
end
end
class CouchUniqueValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless (CouchPotato.database.send(:view, record.class.send("by_#{attribute}", value.to_s.downcase)).blank?)
record.errors[attribute] << (options[:message] || "has already been taken")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment