문제

ActiveRecord의 독창성 검증 자 값이 nil이거나 비어있는 경우 유효성 검사를 건너 뛸 수있는 옵션이 있습니다. 두 매개 변수를 true (기본 동작)로 설정하더라도 유효성 검사를 받기 전에 nil과 blank로 하나의 레코드를 만들 수 있습니다. 기본 SQLITE3 데이터베이스 SQLITE3-RUBY (1.2.5)를 사용합니다.

설명을 위해 편집 : 추가하면 예상 결과를 얻습니다. validates_presence_of 모델에. 나는 기본 동작이라고 생각했다 validates_uniqueness_of 이 중복을 만들 것입니다.

테스트 케이스 :

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

App/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

처음 두 작품이 왜 통과합니까?

감사

도움이 되었습니까?

해결책

당신은 기본 동작에 대해 착각합니다. 에서 문서:

:allow_nil - If set to true, skips this validation if the attribute is nil (default is false).
:allow_blank - If set to true, skips this validation if the attribute is blank (default is false).

두 가지를 모두 True로 설정하면 Rails 2.3.4로 다음 동작이 보입니다.

class Thing < ActiveRecord::Base
  validates_uniqueness_of :identification, :allow_blank => true, :allow_nil => 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">

편집 : 설명 해결.추가 a validates_presence_of 당신이하려는 일에 맞습니다. 완전히 다른 오류 사례를 확인하고 있기 때문에 중복되지 않습니다. 또한 자체 오류 메시지가있어 사용자에게 중요합니다.

class Thing < ActiveRecord::Base
  validates_uniqueness_of :identification, :allow_nil => true, :allow_blank => true
  validates_presence_of :identification
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top