AR中有Person类和PersonName类,具有has_many关系。现在我想验证PersonName模型的格式是字母还是空格。这是代码:
class Person < ActiveRecord::Base
has_many :names, :class_name => "PersonName", :dependent => :destroy
accepts_nested_attributes_for :names
end
class PersonName < ActiveRecord::Base
belongs_to :person
attr_accessible :full, :first, :middle, :last, :maiden, :title, :suffix, :nick
validates_format_of :full, :first, :middle, :last, :maiden, :title, :suffix, :nick, :with => /^[a-zA-Z\s]*$/, :on => :save
end
似乎没有执行validates_format。我跑的时候
PersonName.create(:full => '@#$@').valid?
在控制台中,它返回true。我试图将其更改为:on =&gt; :创建但它仍然返回true。可能是什么问题呢?我是否需要在Person类中指定一些内容?
答案 0 :(得分:0)
来自rails文档
Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line
我认为这是你的问题
答案 1 :(得分:0)