Reputation: 31
I'm using db:populate to preload some sample data into my rails project. For instance, I am using the following code to populate the db:
require 'faker'
namespace :db do
task :populate => :environment do
Rake::Task['db:reset'].invoke
100.times do |u|
User.create!(
:name => Faker::Name.name,
:email => Faker::Internet.email
)
end
puts "The count of user(s) is #{User.all.count}"
User.all.each do |u|
# Add some more info based for each user
end
end
end
However, what I get is an error when I run "rake db:populate". I get:
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
I get this error on the call to puts where I print out the count of users. If I reduce the 100.times down to about 10.times, the populate works correctly and the call to User.all.count responds with the correct value of 10. The best that I can guess is that the call to "faker" gets overloaded and has not yet returned a value which causes the nil object. Maybe however, the populate is trying to run this as a single database transaction and is overloading some buffer.
Is there a way to "flush" the insert into the database so that each transaction is written to the database or pause while "faker" responds so I can create a larger data set to work with?
Thanks
Steve Woolley
[email protected]
Upvotes: 0
Views: 324
Reputation: 1282
I had the same problem
just running rake db:seed on a few console type expressions, after doing a trace it says: ** Invoke environment (first_time) ** Execute environment
Now I don't have an environment 'first_time' I looked at what I changed and it turns out I was calling by default a different environment in the initializer when attempting to connect to the db.
Take a look at your database.yml or application.rb files there might be a conflict
Cheers
Upvotes: 1