I am a noobie. I am trying to generate fake data with faker gem using rake populate task I am getting error Validation failed User must exist. Here is my task file:
namespace :db do
desc "Fill database with test data"
task populate: :environment do
10.times do |u|
name = Faker::LordOfTheRings.character
email = Faker::Internet.email
password = "password"
User.create!( name: name,
email: email,
password: password,
password_confirmation: password)
end
User.all.each do |user|
10.times do |n|
title = Faker::Lorem.sentence
body = Faker::Lorem.paragraphs(1)
created_at = 2.years.ago..Time.now
user_id = user.id
Post.create!(
title: title,
body: body,
created_at: created_at,
user_id: user_id
)
end
end
end
end
And i need to have the user references in my post table as well. Thanks in advance