我正在做hartle教程的第12章。当我运行bundle exec rake db:seed
时出现此错误:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
我试着跑
rake db:reset
rake db:migrate
rake db:test:prepare
最后
rake db:populate
但他们没有解决问题。当我运行rake db:populate
时,它会给出:
Don't know how to build task 'db:populate'
这是我的seeds.rb文件:
# Users
User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
end
# Microposts
users = User.order(:created_at).take(6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
# Following relationships
users = User.all
user = users.first
following = users[2..50]
followers = users[3..40]
following.each { |followed| user.follow(followed) }
followers.each { |follower| follower.follow(user) }
我想问题可能是这一行email = "example-#{n+1}@railstutorial.org"
答案 0 :(得分:-1)
尝试使用:
如果案例已经是现有的电子邮件,它将解决它。
email = "example-#{rand(100000)}@railstutorial.org"
您还可以看到错误:
user = User.new(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar",
admin: true,
activated: true,
activated_at: Time.zone.now)
user.errors
user.save if user.valid
答案 1 :(得分:-1)
你的Gemfile中是否安装了faker和populator?这很可能是问题的一部分。确保你已经运行:
gem install populator #From the command line
并将其包含在您的Gemfile中:
gem 'populator'
以下是Git repo https://github.com/ryanb/populator/tree/master
的链接这里还有很棒的文章:http://sudharti.github.io/articles/using-faker-and-populator-rails/