我尝试使用rake db:seed
使用种子数据填充数据库但由于某种原因我一直收到此错误。注意,我需要绕过验证检查。
01_user.rb
user = User.new([
{id: 6, email: "admin@example.com"},
{id: 7, email: "superadmin@example.com"}
])
user.save!(validate: false)
我一直在收到错误:
$ bundle exec rake db:seed RAILS_ENV=test
rake aborted!
ArgumentError: When assigning attributes, you must pass a hash as an argument.
关于为什么会发生这种情况以及如何解决问题的任何指导?
答案 0 :(得分:1)
您正在将一组哈希值传递给new
模型的User
方法。你需要在每个哈希上循环:
[
{id: 6, email: "admin@example.com"},
{id: 7, email: "superadmin@example.com"}
].each do |user_attributes|
user = User.new(user_attributes)
user.save!(validate: false)
end
我认为你不能覆盖id
属性。
答案 1 :(得分:0)
你也可以试试这个:
email_list = ["admin@example.com", "superadmin@example.com"]
email_list.each do |email_address|
User.create( email: email_address )
end