播种数据库时没有任何反应

时间:2015-10-08 10:06:06

标签: ruby-on-rails activerecord

在我的Rails应用程序中,我有一个简单的seeds.rb

employee_list = [
  ["Kerckhaert", "Martijn", "M", "Pacificatiestraat 22", "Antwerpen", "2000", "0468130781", "033691307", "martijn_kerckhaert@hotmail.com", "martijnmartijn", "martijnmartijn"],
  ["van Lent", "Matthias", "M", "Dorpstraat", "Belsele", "9111", "029301293", "129302193012", "matthias@hotmail.com", "matthiasmatthias", "matthiasmatthias"]]


employee_list.each do |name, firstname, sex, street, city, zip, country, phone, mobile, email, password, password_confirmation|
  Employee.create(name: name, firstname: firstname, sex: sex, city: city, zip: zip, country: country, phone: phone, mobile: mobile, email: email, password: password, password_confirmation: password_confirmation, birthdate: Date.today)
end

但是当我运行rake db:seed时,它什么也没做。

rake db seed

2 个答案:

答案 0 :(得分:1)

您的数组似乎缺少country,这可能会使验证失败,因为2000现在作为国家/地区传递,martijn_kerckhaert@hotmail.com作为移动设备等等。

正如评论中提到的那样,您可以使用create!来引发错误。这将有助于您解决问题。

你也可能最好使用哈希,因为它更具可读性和健壮性。 E.g:

employees = [
  { name: "Kerckhaert",
    firstname: "Martijn",
    sex: "M",
    street: "Pacificatiestraat 22",
    city: "Antwerpen",
    country: "Belgium",
    zip: "2000",
    phone: "0468130781",
    mobile: "033691307",
    email: "martijn_kerckhaert@hotmail.com",
    password: "martijnmartijn",
    password_confirmation: "martijnmartijn" }
]

employees.each { |employee| Employee.create!(employee) }

答案 1 :(得分:1)

我可以在这里建议:

create方法返回true / false值,然后无法让你知道该对象是否正在保存。

而不是使用create,你应该使用create!在这里,您将了解验证错误。