问题
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