diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-05-19 21:49:55 -0700 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-05-19 21:49:55 -0700 |
commit | f7d01ecf753ab8db3ba1b0a10586590b0855a31a (patch) | |
tree | f45fe9618360aee66dcb85c088d424c8597fefc6 /activerecord/test/cases/validations | |
parent | c5205041f65206a1490063b08915e7b2a2151a61 (diff) | |
download | rails-f7d01ecf753ab8db3ba1b0a10586590b0855a31a.tar.gz rails-f7d01ecf753ab8db3ba1b0a10586590b0855a31a.tar.bz2 rails-f7d01ecf753ab8db3ba1b0a10586590b0855a31a.zip |
Fix `validates_uniqueness_off :field, :allow_nil => false`
Closes (#5853)
Uniqueness validator was not properly checking if there are any existing
records, when value was `nil` and column was text type. `nil` was
converted to string, which resulted in queries looking like:
```sql
SELECT 1 FROM "posts" WHERE "posts"."title" = '' LIMIT 1
```
instead of
```sql
SELECT 1 FROM "posts" WHERE "posts"."title" IS NULL LIMIT 1
```
Diffstat (limited to 'activerecord/test/cases/validations')
-rw-r--r-- | activerecord/test/cases/validations/uniqueness_validation_test.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 0f1b3667cc..8708117129 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -293,4 +293,15 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert w6.errors[:city].any?, "Should have errors for city" assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city" end + + def test_allow_nil_is_false + Topic.validates_uniqueness_of(:title, :allow_nil => false) + Topic.destroy_all + + Topic.create!("title" => nil) + topic = Topic.new("title" => nil) + assert !topic.valid?, "topic should not be valid" + assert topic.errors[:title].any?, "Should have errors for title" + assert_equal ["has already been taken"], topic.errors[:title], "Should have uniqueness message for title" + end end |