Validates_Uniqueness_Of يمر على NIL أو BLAGE (دون السماح_NIL والسماح_LANK)

StackOverflow https://stackoverflow.com/questions/1480227

سؤال

المصدق تفرد Activerecord لديه خيارات لتخطي التحقق من الصحة إذا كانت القيمة NIL أو فارغة. حتى لو قمت بتعيين كلا المعلمين إلى True (السلوك الافتراضي)، يمكنني إنشاء سجل واحد مع NIL و BLAGE قبل أن يضرب التحقق من الصحة. يمكنني استخدام قاعدة بيانات 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

محتوى التطبيق / النماذج / thing.rb:

class Thing < ActiveRecord::Base
  validates_uniqueness_of :identification
end

نقل القضبان:

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، أرى السلوك التالي مع القضبان 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