Permalink
Browse files

Fixed nil field value uniqueness check

  • Loading branch information...
1 parent 8f309e3 commit 0fab103cfd6ad2cd0a49e23b88f326ac3d6a9a3e @pyromaniac pyromaniac committed Jul 28, 2011
View
2 activerecord/lib/active_record/validations/uniqueness.rb
@@ -54,7 +54,7 @@ def find_finder_class_for(record) #:nodoc:
def build_relation(klass, table, attribute, value) #:nodoc:
column = klass.columns_hash[attribute.to_s]
- value = column.limit ? value.to_s[0, column.limit] : value.to_s if column.text?
+ value = column.limit ? value.to_s[0, column.limit] : value.to_s if value && column.text?
if !options[:case_sensitive] && value && column.text?
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
View
12 activerecord/test/cases/validations/uniqueness_validation_test.rb
@@ -45,6 +45,18 @@ def test_validate_uniqueness
assert t2.save, "Should now save t2 as unique"
end
+ def test_validates_uniqueness_with_nil_value
+ Topic.validates_uniqueness_of(:title)
+
+ t = Topic.new("title" => nil)
+ assert t.save, "Should save t as unique"
+
+ t2 = Topic.new("title" => nil)
+ assert !t2.valid?, "Shouldn't be valid"
+ assert !t2.save, "Shouldn't save t2 as unique"
+ assert_equal ["has already been taken"], t2.errors[:title]
+ end
+
def test_validates_uniqueness_with_validates
Topic.validates :title, :uniqueness => true
Topic.create!('title' => 'abc')

0 comments on commit 0fab103

Please sign in to comment.