2009-09-26 10 views
49

The uniqueness validator of ActiveRecord में मान शून्य या खाली होने पर सत्यापन को छोड़ने के विकल्प हैं। भले ही मैं दोनों पैरामीटर को सत्य (डिफ़ॉल्ट व्यवहार) पर सेट करता हूं, फिर भी मैं सत्यापन हिट से पहले शून्य और रिक्त के साथ एक रिकॉर्ड बना सकता हूं। मैं डिफ़ॉल्ट SQlite3 डेटाबेस sqlite3-ruby (1.2.5) का उपयोग करता हूं।validates_uniqueness_of शून्य या खाली (allow_nil और allow_blank के बिना)

स्पष्टीकरण के लिए संपादित करें: यदि मैं validates_presence_of मॉडल में जोड़ता हूं तो मुझे अपेक्षित परिणाम मिलते हैं। मैंने सोचा कि validates_uniqueness_of का डिफ़ॉल्ट व्यवहार यह अनावश्यक बना देगा।

testcase:

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 

क्यों पहले दो कृतियों पारित करते हैं?

धन्यवाद

+1

+1 क्योंकि यह एक प्रश्न पूछने का एक मॉडल है। आपने स्पष्ट रूप से बताया कि आपने क्या किया, आपने क्या देखा, और आपने क्या उम्मीद की। – jdl

+0

धन्यवाद। लेकिन ऐसा लगता है कि यह गलत समझा जाने से नहीं रोकता है। :) – Roman

उत्तर

89

आप डिफ़ॉल्ट व्यवहार के बारे में गलत हैं। the docs से:

: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). 

सच करने के लिए उन दोनों को सेट करना, मैं रेल 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 
+0

वास्तव में मैं इसे दूसरी तरफ चाहता हूं। मैं शून्य और खाली मूल्यों को पूरी तरह से रोकना चाहता हूं। एक अतिरिक्त validates_presence_of की तरह होगा। लेकिन मैंने सोचा कि validates_presence अनावश्यक होगा यदि पहले से ही validates_uniqueness_of validator है। – Roman

+0

ठीक है अब यह समझ में आता है। वास्तव में एक शून्य और खाली वास्तव में अद्वितीय हो सकता है। अगर मैं खाली मूल्यों को रोकना चाहता हूं तो मुझे इसे स्पष्ट रूप से कहना होगा। सबसे पहले मैंने इसे परेशान पाया। अब, एक दूसरे विचार पर, मैं इसे और भी पसंद करता हूं। मुझे लगता है कि मैंने अभी अपनी सोच सही दिशा में एक और बहुत छोटा कदम उठाया है। धन्यवाद। – Roman

+9

': allow_blank => true' में शून्य मान शामिल हैं। तो एक अतिरिक्त ': allow_nil => true' अनावश्यक है। – wdspkr

संबंधित मुद्दे