validates_uniqueness_of passes on nil or blank (without allow_nil and allow_blank)

The uniqueness validator of ActiveRecord has an options to skip validation if the value is nil or blank. Even if I set both parameters to true (the default behaviour) I can create one record with nil and blank before the validation hits. I use the default SQlite3 Database sqlite3-ruby (1.2.5).

Edit for clarification: I get the expected result if I add validates_presence_of to the Model. I thought that the default behaviour of validates_uniqueness_of would make this redundant.

Testcase:

rails validation_test
cd validation_test/
script/generate Model Thing identification:string
rake db:migrate

Content of app/models/thing.rb:

class Thing < ActiveRecord::Base
validates_uniqueness_of :identification
end

Rails console:

script/console
Loading development environment (Rails 2.3.4)
>> Thing.create!
=> #<Thing id: 1, identification: nil, created_at: "2009-09-26 01:49:32", updated_at: "2009-09-26 01:49:32">
>> Thing.create! :identification => ""
=> #<Thing id: 2, identification: "", created_at: "2009-09-26 01:49:42", updated_at: "2009-09-26 01:49:42">
>> Thing.create! :identification => ""
ActiveRecord::RecordInvalid: Validation failed: Identification has already been taken
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:1090:in `save_without_dirty!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/dirty.rb:87:in `save_without_transactions!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:200:in `save!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:182:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:200:in `save!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/transactions.rb:200:in `save!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:1059:in `create!'
from (irb):3
>> Thing.count
=> 2

Why do the first two creations pass?

Thanks

47732 次浏览

您对默认行为的判断是错误的:

:allow_nil-如果设置为 true,则在属性为 nil 时跳过此验证(默认为 false)。 :allow_blank-如果设置为 true,则在属性为空时跳过此验证(默认为 false,也包括零)。

如果将 allow_blank设置为 true,Rails 2.3.4将出现以下行为。

class Thing < ActiveRecord::Base
validates_uniqueness_of :identification, :allow_blank => true
end


>> Thing.create! :identification => ""
=> #<Thing id: 6, identification: "", created_at: "2009-09-26 03:09:48", updated_at: "2009-09-26 03:09:48">
>> Thing.create! :identification => ""
=> #<Thing id: 7, identification: "", created_at: "2009-09-26 03:09:49", updated_at: "2009-09-26 03:09:49">
>> Thing.create! :identification => nil
=> #<Thing id: 8, identification: nil, created_at: "2009-09-26 03:09:52", updated_at: "2009-09-26 03:09:52">
>> Thing.create! :identification => nil
=> #<Thing id: 9, identification: nil, created_at: "2009-09-26 03:09:53", updated_at: "2009-09-26 03:09:53">

编辑: 解决你的澄清。

添加 validates_presence_of对于您正在尝试执行的操作是正确的。它不是多余的,因为它检查的是完全不同的错误情况。它还有自己的错误消息,这对用户来说很重要。

class Thing < ActiveRecord::Base
validates_uniqueness_of :identification, :allow_blank => true
validates_presence_of :identification
end

以及 validates:

validates :email, uniqueness: { allow_blank: true }
# or
validates :email, uniqueness: { allow_nil: true }
class Thing < ActiveRecord::Base
validates :identification, uniqueness: true, allow_nil: true
end