2012-01-10 41 views

回答

1

它們都做同樣的事情,validates_confirmation_of只是validates的許多簡單幫助器方法之一。

如果你看看源代碼,你可以看到validates方法只是用來處理所有驗證而validates_confirmation_of明確使用ConfirmationValidator

我覺得validates_confirmation_of在Rails的推出2所以它的存在了相當長的而。

源比較:

def validates_confirmation_of(*attr_names) 
    validates_with ConfirmationValidator, _merge_attributes(attr_names) 
    end 

def validates(*attributes) 
    defaults = attributes.extract_options! 
    validations = defaults.slice!(*_validates_default_keys) 

    raise ArgumentError, "You need to supply at least one attribute" if attributes.empty? 
    raise ArgumentError, "You need to supply at least one validation" if validations.empty? 

    defaults.merge!(:attributes => attributes) 

    validations.each do |key, options| 
     # LOOK AT THIS LINE 
     key = "#{key.to_s.camelize}Validator" 

     begin 
     validator = key.include?('::') ? key.constantize : const_get(key) 
     rescue NameError 
     raise ArgumentError, "Unknown validator: '#{key}'" 
     end 

     # AND LOOK AT THIS LINE 
     validates_with(validator, defaults.merge(_parse_validates_options(options))) 
    end 
    end