我正在尝试对模型应用格式验证,但是当我创建模型时,它并没有返回为无效。我添加了一个长度验证,它工作得很好。我究竟做错了什么?
require 'rubygems'
require 'active_record'
class User < ActiveRecord::Base
validates :username, :format => { :with => /[A-Za-z]+/, :message => "Only letters a-z are allowed" }
validates :username, :length => { :maximum => 20, :too_long => "%{count} letters is too many"}
end
ActiveRecord::Base.establish_connection( ... )
user = User.create!(:username => 'johnsmith1234', :signupdate => '2010-11-12')
puts user.valid?
除非我的长度超过 20 个字符,否则输出始终为真,然后我收到长度错误。那么为什么格式验证不会触发呢?