Ruby-on-rails 在 Rails 中调用自定义验证方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1611887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:53:00  来源:igfitidea点击:

calling custom validation methods in Rails

ruby-on-railsvalidation

提问by cgr

I just upgraded my rails to 2.3.4 and I noticed this with validations: Lets say I have a simple model Company which has a name. nothing to it. I want to run my own validation:

我刚刚将我的 rails 升级到 2.3.4 并且我通过验证注意到了这一点:假设我有一个简单的模型公司,它有一个名字。没什么。我想运行我自己的验证:

class Company < ActiveRecord::Base

  validate :something


  def something
    false
  end

end

saving the model actually works in this case. The same thing happens if i override validate() and return false. I noticed this in a more complex model where my validation was returning false, but the object was still saving...I tried it out in an essentially empty model and the same thing applied. Is there a new practice I am missing? This doesn't seem to be the case in some of my older rails code.

在这种情况下,保存模型实际上有效。如果我覆盖 validate() 并返回 false,也会发生同样的事情。我在一个更复杂的模型中注意到了这一点,其中我的验证返回 false,但对象仍在保存……我在一个基本上为空的模型中进行了尝试,并应用了相同的方法。有没有我缺少的新练习?在我的一些旧 Rails 代码中,情况似乎并非如此。

回答by Damien MATHIEU

Your validations are executed when you use the validatemethod. However rails doesn't relies on the returned value.

当您使用该validate方法时,将执行您的验证。但是 rails 不依赖于返回值。

It relies on if there are validations errors or not. So you should add errors when your model doesn't validates.

它取决于是否存在验证错误。因此,当您的模型未验证时,您应该添加错误。

def something
    errors.add(:field, 'error message')
end

Or, if the error is not related to a field :

或者,如果错误与字段无关:

def something
    errors.add(:base, 'error message')
end

Then your model won't be saved because there are errors.

那么你的模型将不会被保存,因为有错误。

回答by EmFi

You're getting confused between validations and callbacks.

您在验证和回调之间感到困惑。

Validations are supposed to fail if there are any errors on the object, doesn't matter what the validation returns. Callbacks fail if they return false, regardless if they add any errors to object.

如果对象上有任何错误,验证应该会失败,无论验证返回什么。如果回调返回 false,则回调失败,无论它们是否向对象添加任何错误。

Rails uses calls valid? from save calls which does not check the result of any validations.

Rails 使用调用有效吗?来自不检查任何验证结果的保存调用。

Edit:Rails treats validate :methodas a callback, but valid? still doesn't check for their results, only for errors they added to the object.

编辑:Rails 视为validate :method回调,但有效吗?仍然不检查他们的结果,只检查他们添加到对象中的错误。

I don't think this behaviour changed at all but I could be wrong. I don't think I've ever written a validation to return false before.

我认为这种行为根本没有改变,但我可能是错的。我认为我以前从未编写过返回 false 的验证。

回答by Maged Makled

Just FYI errors.add_to_base('error message')has been deprecated in rails 3 and got replaced by

仅供参考errors.add_to_base('error message')在 rails 3 中已被弃用,并被替换为

errors[:base] << "Error message" 

Or

或者

errors.add(:base, "Error message")