developer tip

validates_uniqueness_of는 nil 또는 공백 (allow_nil 및 allow_blank 제외)에 전달합니다.

copycodes 2020. 12. 8. 08:25
반응형

validates_uniqueness_of는 nil 또는 공백 (allow_nil 및 allow_blank 제외)에 전달합니다.


ActiveRecord의 고유성 유효성 검사기 에는 값이 nil이거나 비어있는 경우 유효성 검사를 건너 뛸 수있는 옵션이 있습니다. 두 매개 변수를 모두 true (기본 동작)로 설정하더라도 유효성 검사가 시작되기 전에 nil과 공백으로 하나의 레코드를 만들 수 있습니다. 기본 SQlite3 Database 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">

Edit: Addressing your clarification. Adding a validates_presence_of would be correct for what you're trying to do. It's not redundant, since it's checking for a completely different error case. It also has its own error message, which will be important for the user.

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

참고URL : https://stackoverflow.com/questions/1480227/validates-uniqueness-of-passes-on-nil-or-blank-without-allow-nil-and-allow-blan

반응형