Rails 3-数据库种子数据验证 [英] Rails 3 - DB seed data validation
问题描述
我正在通过seeds.rb
文件中的数千个create
调用在Rails 3.1中植入测试数据库.
I am seeding a test database in Rails 3.1 through thousands of create
calls in the seeds.rb
file.
当这些调用未通过模型验证时会出现一个小问题:rails不会通知我这一点,并且播种会正确进行直到文件结束.在该过程的最后,我不知道哪些记录已经创建,哪些记录没有创建,除非我逐一检查它们...
A little problem arises when these calls do not pass the model validations: rails will not notify me this, and the seeding goes on correctly until the end of the file. At the end of the process I do not know which records have been created and which aren't, unless I check them one by one ...
当使用rake db:seed
或rake db:reset
的记录未通过验证时,是否有办法得到通知?
Is there a way to get notified when records do not pass validations when using rake db:seed
or rake db:reset
?
谢谢!
推荐答案
您可以在模型中创建所需的验证,然后使用ModelName.create!
.如果输入无效,则会引发异常
You can create validations you want in the Models and use ModelName.create!
. This will raise an exception if the input is invalid
检查此 http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/create!
希望这会有所帮助:)
这篇关于Rails 3-数据库种子数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!