Валидатор уникальности ActiveRecord имеет параметры пропуска проверки, если значение равно нулю или пусто. Даже если я установил оба параметра в true (поведение по умолчанию), я могу создать одну запись с нулем и пробелом до того, как будут проверены проверки. Я использую базу данных SQlite3 по умолчанию sqlite3-ruby (1.2.5).
Изменить для пояснения: я получаю ожидаемый результат, если я добавлю validates_presence_of
в модель. Я думал, что поведение по умолчанию validates_uniqueness_of
сделает это избыточным.
TestCase:
rails validation_test
cd validation_test/
script/generate Model Thing identification:string
rake db:migrate
Содержимое приложения /models/thing.rb:
class Thing < ActiveRecord::Base
validates_uniqueness_of :identification
end
Консоль Rails:
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
Почему проходят первые два творения?
Спасибо