【发布时间】:2015-07-11 06:22:42
【问题描述】:
我正在学习 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"
【问题讨论】:
-
我看不出你的种子文件有问题。您确定数据库正确删除了吗?也许进入 rails c 并检查是否有任何持续存在。或者尝试捕获异常并确定导致问题的记录。
标签: ruby-on-rails ruby railstutorial.org