0

I'm trying to populate data of a db very similar to this thread.

My sample rake looks like this

namespace :db do
   desc "Fill database with sample data"
   users_to_create = 1
   dogs_to_create = users_to_create * 1
   breed_array_size = 177 #0 indexed
   array_zips = ["11211", "11215", "01776"]
      task populate: :environment do
         admin = User.create!(name: "First Last",
                     email: "something@gmail.com",
                     password: "somepass",
                     password_confirmation: "somepass",
                     zip_code: "11211",
                     admin: true)

         users_to_create.times do |n|
                    name  = Faker::Name.name
                    email = "example-#{n+1}@example.com"
                    password  = "password123"
                    zip_code2 = array_zips.sample
                         User.create!(name: name,
                              email: email,
                              password: password,
                              zip_code: zip_code2, 
                              password_confirmation: password)
          end

         dogs_to_create.times do |d|
             primary_breed = 

['affenpinscher'],
['afghan_hound'],
['airedale_terrier'],
['akita'],
['alaskan_malamute'],
['american_english_coonhound'],
['american_eskimo_dog'],
['american_foxhound'],
['american_staffordshire_terrier'],
['american_water_spaniel'],
['anatolian_shepherd_dog'],
['australian_cattle_dog'],
['australian_shepherd'],
['australian_terrier'],
['basenji'],
['basset_hound'],
['beagle'],
['bearded_collie'],
['beauceron'],
['bedlington_terrier'],
['belgian_malinois'],
['belgian_sheepdog'],
['belgian_tervuren'],
['bernese_mountain_dog'],
['bichon_frise'],
['black_russian_terrier'],
['black_and_tan_coonhound'],
['bloodhound'],
['bluetick_coonhound'],
['border_collie'],
['border_terrier'],
['borzoi'],
['boston_terrier'],
['bouvier_des_flandres'],
['boxer'],
['boykin_spaniel'],
['briard'],
['brittany'],
['brussels_griffon'],
['bull_terrier'],
['bulldog'],
['bullmastiff'],
['cairn_terrier'],
['canaan_dog'],
['cane_corso'],
['cardigan_welsh_corgi'],
['cavalier_king_charles_spaniel'],
['cesky_terrier'],
['chesapeake_bay_retriever'],
['chihuahua'],
['chinese_crested'],
['chinese_shar-pei'],
['chinook'],
['chow_chow'],
['clumber_spaniel'],
['cocker_spaniel'],
['collie'],
['curly-coated_retriever'],
['dachshund'],
['dalmatian'],
['dandie_dinmont_terrier'],
['doberman_pinscher'],
['dogue_de_bordeaux'],
['english_cocker_spaniel'],
['english_foxhound'],
['english_setter'],
['english_springer_spaniel'],
['english_toy_spaniel'],
['entlebucher_mountain_dog'],
['field_spaniel'],
['finnish_lapphund'],
['finnish_spitz'],
['flat-coated_retriever'],
['french_bulldog'],
['german_pinscher'],
['german_shepherd_dog'],
['german_shorthaired_pointer'],
['german_wirehaired_pointer'],
['giant_schnauzer'],
['glen_of_imaal_terrier'],
['golden_retriever'],
['gordon_setter'],
['great_dane'],
['great_pyrenees'],
['greater_swiss_mountain_dog'],
['greyhound'],
['harrier'],
['havanese'],
['ibizan_hound'],
['icelandic_sheepdog'],
['irish_red_and_white_setter'],
['irish_setter'],
['irish_terrier'],
['irish_water_spaniel'],
['irish_wolfhound'],
['italian_greyhound'],
['japanese_chin'],
['keeshond'],
['kerry_blue_terrier'],
['komondor'],
['kuvasz'],
['labrador_retriever'],
['lakeland_terrier'],
['leonberger'],
['lhasa_apso'],
['lowchen'],
['maltese'],
['manchester_terrier'],
['mastiff'],
['miniature_bull_terrier'],
['miniature_pinscher'],
['miniature_schnauzer'],
['neapolitan_mastiff'],
['newfoundland'],
['norfolk_terrier'],
['norwegian_buhund'],
['norwegian_elkhound'],
['norwegian_lundehund'],
['norwich_terrier'],
['nova_scotia_duck_tolling_retriever'],
['old_english_sheepdog'],
['otterhound'],
['papillon'],
['parson_russell_terrier'],
['pekingese'],
['pembroke_welsh_corgi'],
['petit_basset_griffon_vendeen'],
['pharaoh_hound'],
['plott'],
['pointer'],
['polish_lowland_sheepdog'],
['pomeranian'],
['poodle'],
['portuguese_podengo_pequeno'],
['portuguese_water_dog'],
['pug'],
['puli'],
['pyrenean_shepherd'],
['rat_terrier'],
['redbone_coonhound'],
['rhodesian_ridgeback'],
['rottweiler'],
['russell_terrier'],
['saluki'],
['samoyed'],
['schipperke'],
['scottish_deerhound'],
['scottish_terrier'],
['sealyham_terrier'],
['shetland_sheepdog'],
['shiba_inu'],
['shih_tzu'],
['siberian_husky'],
['silky_terrier'],
['skye_terrier'],
['smooth_fox_terrier'],
['soft_coated_wheaten_terrier'],
['spinone_italiano'],
['st._bernard'],
['staffordshire_bull_terrier'],
['standard_schnauzer'],
['sussex_spaniel'],
['swedish_vallhund'],
['tibetan_mastiff'],
['tibetan_spaniel'],
['tibetan_terrier'],
['toy_fox_terrier'],
['treeing_walker_coonhound'],
['vizsla'],
['weimaraner'],
['welsh_springer_spaniel'],
['welsh_terrier'],
['west_highland_white_terrier'],
['whippet'],
['wire_fox_terrier'],
['wirehaired_pointing_griffon'],
['xoloitzcuintli'],
['yorkshire_terrier']


        user_id = 1
        name = Faker::Name.name
        nick = Faker::Name.name[0..2]
        primarybreed = primary_breed[d].join('')
        secondarybreed = "Purebred"
        age = rand(1..12)
           Dog.create!(user_id: user_id,
              name: name,
              nick: nick,
              age: age,
              primarybreed: primarybreed,
              secondarybreed: secondarybreed
              )
           end
     Dog.all.each { |dog| dog.image=File.open(Dir.glob(File.join(Rails.root,  "public/images/fake_dogs/#{dog.primarybreed}", '*')).sample); dog.save! }

   end

 end

The error I get is

TypeError: no implicit conversion of nil into String

it points to

 Dog.all.each { |dog| dog.image=File.open(Dir.glob(File.join(Rails.root,  "public/images/fake_dogs/#{dog.primarybreed}", '*')).sample); dog.save! }

If I tried to test with this line:

 Dog.all.each { |dog| dog.image=File.open(Dir.glob(File.join(Rails.root,  "public/images/test/", '*')).sample); dog.save! }

and it works (with test folder having a few images), but with the first method, it's breaking. If I comment out this line for test purposes, and create the dog without the image, and then go into rails console.

Dog.first.primarybreed 

returns the breed, so it's not that the breed that is nil, and there ARE images in the (first 20 breed-folders, and I'm just trying to create one dog in this example which would be the first breed, which does have images )

What is nil here? I've been working on this for a while... maybe losing my mind.

Thanks in advance and sorry for the long array, but I didn't want to nix it in case there's a problem there.

Community
  • 1
  • 1
Peege151
  • 1,562
  • 2
  • 21
  • 45
  • I'd guess that the strings you are supplying are not the issue. I would be curious what the Faker::Name.name looks like. To me this looks like the most obvious nil case but there is also a weird User.create block in the user_to_create.times that I am not quite sure of as well. – Dan Bradbury May 04 '14 at 16:36
  • The issue is really only with Dog images created at the end. I've set user_to_create at the top so I can just change variables at the top to avoid going swimming through my code. – Peege151 May 04 '14 at 16:45

1 Answers1

2

As per the chat session with OP,

OP had created the folders for primarybreed with a perl command

perl -lne 'mkdir $_' dirnames.txt

dirnames.txt had a list of folder names to be created. There was an extra space in all the folder names listed in dirnames.txt

So upon trying to access with dog.primarybreed(without a space) in following command

 Dog.all.each { |dog| dog.image=File.open(Dir.glob(File.join(Rails.root,  "public/images/fake_dogs/#{dog.primarybreed}", '*')).sample); dog.save! }

dog.primarybreed(without space) was not matching the actual folder name(with space) which caused

Dir.glob(File.join(Rails.root, "public/images/fake_dogs/#{dog.primarybreed}", '*'))

to return an empty array.

When you sample an empty array, you get nil

2.1.0 :001 > [].sample
 => nil 

Finally, nil was passed to File.open(nil) which resulted in error

TypeError: no implicit conversion of nil into String

To fix this issue, all the folders for primarybreed were named correctly and the extra character was removed from name.

Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • Kirti - Give the link of the chat to the answer too. It is well allowed. Otherwise it is very hard to match the answer with the post. – Arup Rakshit May 04 '14 at 17:53
  • Arup.. Answer is self explanatory. I don't think anyone would have any problem understanding or matching the issue or how it was resolved. It's a simple directory name mismatch issue, I simply gave a thorough explanation. Here is the link to chat if anyone is interested http://chat.stackoverflow.com/rooms/48530/ror – Kirti Thorat May 04 '14 at 18:39
  • @user3317140 sorry XD – Peege151 May 04 '14 at 18:47
  • I'm glad this was the issue though, I was driving myself nuts. I should've picked up on the mismatch earlier. – Peege151 May 04 '14 at 18:48
  • No worries, It happens. And Glad to help :) – Kirti Thorat May 04 '14 at 18:53