web-dev-qa-db-ja.com

validates_uniqueness_ofはnilまたは空白を渡します(allow_nilおよびallow_blankなし)

ActiveRecordの一意性検証ツール には、値がnilまたは空白の場合に検証をスキップするオプションがあります。両方のパラメーターをtrue(デフォルトの動作)に設定しても、検証がヒットする前に、nilと空白の1つのレコードを作成できます。デフォルトの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

最初の2つの作成が成功するのはなぜですか?

ありがとう

59
Roman

デフォルトの動作について間違えています。 ドキュメント から:

: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">

編集:説明に対処します。validates_presence_ofはあなたがやろうとしていることに対して正しいでしょう。完全に異なるエラーケースをチェックしているため、冗長ではありません。また、独自のエラーメッセージがあり、ユーザーにとって重要です。

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