Rails 3.2如何仅为新记录调用before_validation

jam*_*esc 2 sti ruby-on-rails-3

Rails 3.x before_validation(:on => :create) do不起作用.任何其他想法赞赏

小智 6

您可以使用以下内容向回调添加条件:

before_validation :do_something, :unless => Proc.new { |model| model.persisted? }
Run Code Online (Sandbox Code Playgroud)


小智 6

这就是我做得很好的作品.

before_validation do
  if self.new_record?      
    # do something here
  end
end
Run Code Online (Sandbox Code Playgroud)