我目前正在浏览 RoR 指南,但我被困在...
“向样本数据添加关注/关注者关系。”
这是假设可以工作的代码:sample_app/lib/task/sample_data.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
make_users
make_microposts
make_relationships
end
end
def make_users
admin = User.create!(name: "Example User2",
email: "example2@railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
admin.toggle!(:admin)
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)
end
end
def make_microposts
users = User.all(limit: 6)
50.times do
content = Faker::Lorem.sentence(5)
users.each { |user| user.microposts.create!(content: content) }
end
end
def make_relationships
users = User.all
user = users.first
followed_users = users[2..50]
followers = users[3..40]
followed_users.each { |followed| user.follow!(followed) }
followers.each { |follower| follower.follow!(user) }
end
当我rake db:reset
重置数据库时没有问题。
当我这样做时rake db:populate
发生错误说明:
rake aborted!
Validation failed: Follower can't be blank`
所以我检查了我的数据库,除了“关系”表之外,所有表都被填充了。有什么想法或建议吗?我很确定代码有问题,def making_relationships
确切地说。希望有人对此有解决方案..
-马克