找到你要的答案

Q:Nothing happens when seeding database

Q:播种数据库时没有发生

In my Rails application I have a simple 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

But when I run rake db:seed it does nothing.

我这里有一个简单的seeds.rb Rails应用程序:

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

但当我运行耙DB:种子它什么也不做。

answer1: 回答1:

It looks like your array is missing country, which likely makes the validations fail, because 2000 is now passed as the country, martijn_kerckhaert@hotmail.com as the mobile, etc.

As someone mentioned in the comments, you can use create! to raise an error when they do. This will help you trouble shoot.

You might also be better off using hashes for this, as it is more readable, and robust. 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) }

它看起来像你的阵列丢失的国家,这可能使验证失败,因为2000是现在国家通过,martijn_kerckhaert@hotmail.com作为移动,等等。

正如评论中提到的那样,你可以使用!当他们做错误时。这将有助于你麻烦拍摄。

你也可能会得到更好的使用的哈希值,因为它是更具可读性,和强大的。例如.:

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) }
answer2: 回答2:

What I can suggest here:

create method returns true/false value then there is no way to letting you know that object is saving or not.

instead of using create, you should use create! here so that you will know about the validation errors.

我可以建议在这里:

创建方法返回true / false值,那么就没有办法让你知道该对象是保存或不保存。

而不是使用创建,你应该使用创建!在这里,你会知道验证错误。

ruby-on-rails  activerecord