ker*_*lin 15 validation ruby-on-rails
我想在创建记录之前执行自定义验证?
看起来这是正确的方法:before_validation_on_create.例如:
before_validation_on_create :custom_validation
Run Code Online (Sandbox Code Playgroud)
但我不确定.任何帮助,将不胜感激.
sme*_*mek 32
在轨道3
before_validation_on_create :do_something
Run Code Online (Sandbox Code Playgroud)
已被替换为:
before_validation :do_something, :on => :create
Run Code Online (Sandbox Code Playgroud)
Joh*_*hir 19
before_validation_on_create挂钩在创建验证之前发生...但它们本身不是验证.
您可能想要做的是使用validate
和添加到错误数组的私有方法.像这样:
class IceCreamCone
validate :ensure_ice_cream_is_not_melted, :before => :create
private
def ensure_ice_cream_is_not_melted
if ice_cream.melted?
errors.add(:ice_cream, 'is melted.')
end
end
end
Run Code Online (Sandbox Code Playgroud)
以下内容在Rails 5中为我工作:
validate :custom_validation_method, :on => :create
跑步
validate :custom_validation_method, :before => :create
给了我以下错误:
Unknown key: :before. Valid keys are: :on, :if, :unless, :prepend.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13106 次 |
最近记录: |