Rails 3 中无法跳过验证吗?
我正在 Rails 3 中的一个项目中工作,我需要创建一个空记录,将其保存到数据库而不进行验证(因为它是空的),然后允许用户编辑此记录以完成它,并从然后就出去了。
现在我遇到了一个非常基本的问题:在任何情况下,我似乎都无法在不验证模型的情况下保存模型。
我在控制台中尝试了以下操作:
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
然后尝试将模型中的所有验证更改为 :on =>; :更新
。任何尝试保存时都会出现相同的错误消息。
那么我在这里缺少什么?如何创建一个空记录,然后在用户编辑它时进行验证?
谢谢!
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.
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.
So what am I missing here? How can I create an empty record and then let validation occur as the user edits it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过正常用例保存无效模型是一种不好的做法。请改用条件验证:
或者如果有很多条件验证:
这样就可以进行夜间完整性测试,检查所有记录的有效性。
无需验证即可保存的有效用例是测试边缘情况,例如测试数据库约束是否得到执行。
It is a bad practice to have invalid models saved by normal use cases. Use conditional validations instead:
or if you have many:
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.
*叹息...*
发现问题...我的 after_validate 方法调用之一是添加信息并重新保存模型,因此我收到的错误不是来自控制台输入,它们来自正在保存的 after_validate 方法再次。
谢谢大家。
*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.
Thanks all.
仅适用于紧急情况
假设您已经非常仔细地考虑了这一点并且确定这是一个好主意,则可以在不进行验证的情况下使用以下方法进行保存:
几乎没有有效的用例,因此应将其视为紧急一次性程序。您的用例不符合条件。
无效记录的问题
数据库中存在无效记录会导致各种问题。例如,您向所有用户发送一封电子邮件并更新用户模型上的“last_contacted_at”字段。您的无效用户将不会更新,并将陷入电子邮件死亡螺旋。
条件验证
正如其他发帖者所指出的,条件验证将解决您可能使用 validate: false 来解决的大多数问题。
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:
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.
Conditional validation
As other posters have pointed out, conditional validation will solve most issues for which you might otherwise have used validate: false.
不要将无效模型放入数据库中,而是存储部分完成的模型(使用
Model.new
创建)在会话中。只有在完全有效的情况下才将其保存到数据库中。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.