質問

スキップするオプションがありのActiveRecordのの

一意性検証ツール値がnilまたは空白の場合、検証。私は真(デフォルトの動作)に両方のパラメータを設定しても、私は、検証のヒットの前にゼロと空白と1つのレコードを作成することができます。私は、デフォルトのsqlite3のデータベースのsqlite3の-ルビー(1.2.5)を使用します。

明確化のための編集:私はモデルにvalidates_presence_ofを追加した場合、私は期待どおりの結果を得ることができます。私はvalidates_uniqueness_ofのデフォルトの動作は、この冗長になるだろうと思っています。

テストケース:

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

アプリ/モデル/ 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点の作品が通過していますか?

おかげ

役に立ちましたか?

解決

あなたはデフォルトの動作について誤解されています。 ドキュメントするます:

: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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top