diff options
author | pyromaniac <kinwizard@gmail.com> | 2011-07-28 19:09:12 +0400 |
---|---|---|
committer | pyromaniac <kinwizard@gmail.com> | 2012-01-06 12:56:04 +0700 |
commit | 0fab103cfd6ad2cd0a49e23b88f326ac3d6a9a3e (patch) | |
tree | b3c88eb72fddee028f5616f09d999be17db4c150 /activerecord | |
parent | 8f309e31057e1b26fefedb199ab0526126fb1fe4 (diff) | |
download | rails-0fab103cfd6ad2cd0a49e23b88f326ac3d6a9a3e.tar.gz rails-0fab103cfd6ad2cd0a49e23b88f326ac3d6a9a3e.tar.bz2 rails-0fab103cfd6ad2cd0a49e23b88f326ac3d6a9a3e.zip |
Fixed nil field value uniqueness check
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/validations/uniqueness_validation_test.rb | 12 |
2 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index d56248b928..c10611ea00 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -54,7 +54,7 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 382ad0a06a..327830096b 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -45,6 +45,18 @@ class UniquenessValidationTest < ActiveRecord::TestCase 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') |