Ruby-on-rails 无法跳过 Rails 3 中的验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4694649/
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-03 00:05:45  来源:igfitidea点击:

Cannot skip validation in Rails 3?

ruby-on-railsvalidationruby-on-rails-3modelruntime-error

提问by Andrew

I'm working on a project in Rails 3 where I need to create an empty record, save it to the database without validation (because it's empty), and then allow the users to edit this record in order to complete it, and validate from then on out.

我正在 Rails 3 中处理一个项目,我需要创建一个空记录,将其保存到数据库中而不进行验证(因为它是空的),然后允许用户编辑此记录以完成它,并从然后出去。

Now I've run into a pretty basic problem: I can't seem to save a model without validating it under any circumstances.

现在我遇到了一个非常基本的问题:在任何情况下我似乎都无法在没有验证的情况下保存模型。

I've tried the following in the console:

我在控制台中尝试了以下内容:

model = Model.new
model.save(false) # Returns RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
model.save( :validate => false ) # Returns same error as above

model = Model.create
model.save(false) # Same runtime error
model.save( :validate => false ) # Same runtime error

I then tried changing all the validations in the model to :on => :update. Same error messages on any attempt to save.

然后我尝试将模型中的所有验证更改为:on => :update. 任何尝试保存时都会出现相同的错误消息。

So what am I missing here? How can I create an empty record and then let validation occur as the user edits it?

那么我在这里错过了什么?如何创建一个空记录,然后在用户编辑它时进行验证?

Thanks!

谢谢!

采纳答案by Andrew

*sigh...*

*叹...*

Found the problem... one of my after_validate method calls was adding information and resaving the model, hence the errors I was getting weren't from the console input, they were coming from the after_validate method which was saving again.

发现问题...我的 after_validate 方法调用之一是添加信息并重新保存模型,因此我得到的错误不是来自控制台输入,而是来自再次保存的 after_validate 方法。

Thanks all.

谢谢大家。

回答by Leventix

It is a bad practice to have invalid models saved by normal use cases. Use conditional validations instead:

通过正常用例保存无效模型是一种不好的做法。改用条件验证:

validates_presence_of :title, :unless => :in_first_stage?

or if you have many:

或者如果你有很多:

with_options :unless => :in_first_stage? do
  validates_presence_of :title
  validates_presence_of :author
end

This way nothing stands in way to have nightly integrity tests, which checks all records for validity.

通过这种方式,没有什么可以阻止夜间完整性测试,它会检查所有记录的有效性。

A valid use case for saving without validations would be for testing edge cases, e.g. to test that a database constraint is enforced.

无需验证即可保存的有效用例是测试边缘情况,例如测试是否强制执行数据库约束。

回答by superluminary

For emergencies only

仅用于紧急情况

Assuming that you have considered this very carefully and are certain that this is a good idea, you can save without validation using:

假设您已经非常仔细地考虑了这一点并确定这是一个好主意,您可以使用以下方法保存而无需验证:

my_model.save validate: false

There are almost no valid use cases for this, and it should be considered an emergency one off procedure. Your use case does not qualify.

对此几乎没有有效的用例,应将其视为紧急一次性程序。您的用例不符合条件。

Problems with invalid records

无效记录的问题

Having invalid records in the database leads to all manner of problems down the line. For example, you send an email to all users and update a 'last_contacted_at' field on your user model. Your invalid users will not be updated and will descend into an email spiral of death.

数据库中的无效记录会导致各种问题。例如,您向所有用户发送电子邮件并更新用户模型上的“last_contacted_at”字段。您的无效用户将不会更新,并且会陷入死亡的电子邮件漩涡。

Conditional validation

条件验证

As other posters have pointed out, conditional validation will solve most issues for which you might otherwise have used validate: false.

正如其他海报所指出的那样,条件验证将解决大多数您可能会使用 validate: false 的问题。

回答by Andy Lindeman

Instead of placing an invalid model in the database, store the partially completed model (created with Model.new) in a session. Only save it to the database when it is completely valid.

不是在数据库中放置无效模型,而是将部分完成的模型(使用Model.new)存储在 session 中。只有在完全有效时才将其保存到数据库中。