Question

I am following a Ruby on Rails guide and ran into a problem generating fake content using the gem "Faker". I installed faker and followed the instructions to populate users and photos into my project. I created this file in lib/tasks/populate.rake

lib/tasks/populate.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    10.times do |n|
      puts "[DEBUG] creating user #{n+1} of 10"
      name = Faker::Name.name
      email =  "user-#{n+1}@example.com" 
      password = "password"
      User.create!( name: name,
                    email: email,
                    password: password,
                    password_confirmation: password)
    end

    User.all.each do |user|
      puts "[DEBUG] uploading images for user #{user.id} of #{User.last.id}"
      10.times do |n|
        image = File.open(Dir.glob(File.join(Rails.root, 'sampleimages', '*')).sample)
        description = %w(cool awesome crazy wow adorbs incredible).sample
        user.pins.create!(image: image, description: description)
      end
    end
  end
end

Now all I am supposed to do is put "rake db:populate" in my terminal (and in the right folder). When I do this I get:

rake db:populate
[DEBUG] creating user 1 of 10
[DEBUG] creating user 2 of 10
[DEBUG] creating user 3 of 10
[DEBUG] creating user 4 of 10
[DEBUG] creating user 5 of 10
[DEBUG] creating user 6 of 10
[DEBUG] creating user 7 of 10
[DEBUG] creating user 8 of 10
[DEBUG] creating user 9 of 10
[DEBUG] creating user 10 of 10
[DEBUG] uploading images for user 15 of 25
rake aborted!
can't convert nil into String
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `initialize'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `open'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `block (4 levels) in <top (required)>'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:17:in `times'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:17:in `block (3 levels) in <top (required)>'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:15:in `each'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:15:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:populate
(See full trace by running task with --trace)

My big concern here is

"rake aborted! can't convert nil into String"

It appears it created users but did not upload any pictures.

Question:

a) Why is rake being aborted?

b) How can I delete the users I created? The only way I know is" User.all" then User.delete(put a number here)...Are there more efficient ways of doing this?

Help would be greatly appreciated :) Thanks!

Was it helpful?

Solution

Your <rails_root>/sampleimages directory is empty. Dir.glob(...) is returning an empty array and when you call sample it's returning nil. When you call File.open(nil) you get the exception and rake aborts.

If you want to delete all rows in the users table you can do User.destroy_all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top