Skip to content

Instantly share code, notes, and snippets.

@neurodynamic
Created August 5, 2015 15:19
Show Gist options
  • Save neurodynamic/e91883852e17284f6fbf to your computer and use it in GitHub Desktop.
Save neurodynamic/e91883852e17284f6fbf to your computer and use it in GitHub Desktop.
custom scoped attribute uniqueness validation method for Volt framework
def validate_uniqueness_scope(*attribute_array)
attributes_hash = Hash[attribute_array.map { |i| [ i, send(i)] }]
store.send(self.class.name.downcase.pluralize).where(attributes_hash).count.then do |count|
if count > 0
{ self.class.name.downcase.to_sym => ["#{attributes_hash.values.join(', ')} already exists."] }
else
{}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment