Rails 3 - DB seed data validation

为君一笑 提交于 2020-01-02 09:55:56

问题


I am seeding a test database in Rails 3.1 through thousands of create calls in the seeds.rb file.

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 ...

Is there a way to get notified when records do not pass validations when using rake db:seed or rake db:reset?

Thank you!


回答1:


You can create validations you want in the Models and use ModelName.create!. This will raise an exception if the input is invalid

Check this out http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/create!

Hope this helps :)



来源:https://stackoverflow.com/questions/8605366/rails-3-db-seed-data-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!