make uniqueness_of_name validations work again (closes foodcoops/foodsoft#157)

This commit is contained in:
wvengen 2013-07-15 18:03:02 +02:00
parent 3fd498d29f
commit 5e9a49b2c7
2 changed files with 9 additions and 9 deletions

View file

@ -108,10 +108,10 @@ class Ordergroup < Group
# Make sure, the name is uniq, add usefull message if uniq group is already deleted # Make sure, the name is uniq, add usefull message if uniq group is already deleted
def uniqueness_of_name def uniqueness_of_name
id = new_record? ? nil : self.id group = Ordergroup.where('groups.name = ?', name)
group = Ordergroup.where('groups.id != ? AND groups.name = ?', id, name).first group = group.where('groups.id != ?', self.id) unless new_record?
if group.present? if group.exists?
message = group.deleted? ? :taken_with_deleted : :taken message = group.first.deleted? ? :taken_with_deleted : :taken
errors.add :name, message errors.add :name, message
end end
end end

View file

@ -82,10 +82,10 @@ class Supplier < ActiveRecord::Base
# Make sure, the name is uniq, add usefull message if uniq group is already deleted # Make sure, the name is uniq, add usefull message if uniq group is already deleted
def uniqueness_of_name def uniqueness_of_name
id = new_record? ? nil : self.id supplier = Supplier.where('suppliers.name = ?', name)
supplier = Supplier.where('suppliers.id != ? AND suppliers.name = ?', id, name).first supplier = supplier.where('suppliers.id != ?', self.id) unless new_record?
if supplier.present? if supplier.exists?
message = supplier.deleted? ? :taken_with_deleted : :taken message = supplier.first.deleted? ? :taken_with_deleted : :taken
errors.add :name, message errors.add :name, message
end end
end end