在rails中创建期间跳过一些验证,但运行其他验证

San*_*Rao 0 ruby-on-rails ruby-on-rails-3

我想在创建新用户(例如地址,引脚,电话号码等)期间跳过少数属性的验证.
但是,当用户尝试编辑它时,仍需要在模型中执行其他验证.我尝试使用:on =>:update但这对我没有帮助.有什么建议 ?

我的代码:

validates :address, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :city, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :state, :presence => true, :length => { :maximum => 50 }, :on => :update 
validates :zip, :presence => true, :numericality => true, :on => :update, :length => { :is => 5 }
Run Code Online (Sandbox Code Playgroud)

Jus*_*ick 5

根据文档,你需要做的是这样的事情.你是说这不起作用吗?

class Person < ActiveRecord::Base
  validates_presence_of :address, :on => :update
  validates_presence_of :pin,     :on => :update
end
Run Code Online (Sandbox Code Playgroud)