图像和假数据总体 - 收益率为零

时间:2014-05-04 16:32:23

标签: ruby-on-rails ruby

我正在尝试填充与this线程非常相似的数据库。

我的样本rake看起来像这样

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

我得到的错误是

  

TypeError:没有将nil隐式转换为String

指向

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

如果我尝试用这一行测试:

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

它的工作原理(测试文件夹中有一些图像),但是第一种方法,它正在破碎。如果我为了测试目的而注释掉这一行,并创建没有图像的狗,然后进入rails控制台。

Dog.first.primarybreed 

返回品种,所以不是品种是零,并且(前20个品种文件夹中有ARE图像,我只是想在这个例子中创建一只狗,这将是第一个品种,哪个有图像)

这里有什么?我已经做了一段时间......可能会失去理智。

提前感谢并为长阵列感到抱歉,但是如果那里出现问题我不想修改它。

1 个答案:

答案 0 :(得分:2)

根据与OP的聊天会话,

OP已使用primarybreed命令

perl创建了文件夹
perl -lne 'mkdir $_' dirnames.txt

dirnames.txt有一个要创建的folder个名字的列表。 dirnames.txt

中列出的所有文件夹名称中都有一个额外的空格

所以尝试使用dog.primarybreed(没有空格)访问以下命令

 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(没有空格)实际文件夹名称(带空格)不匹配导致

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

返回一个空数组。

当您sample为空数组时,您会得到nil

2.1.0 :001 > [].sample
 => nil 

最后,nil被传递给File.open(nil),导致错误

TypeError: no implicit conversion of nil into String

要解决此问题,primarybreed的所有文件夹正确命名额外字符已从名称中移除