# Upload test images using rake with populator

**URL:** <https://www.ruby-forum.com/t/upload-test-images-using-rake-with-populator/154843>\
**Category:** Rails\
**Created:** [December 4, 2008, 12:34pm UTC](https://www.ruby-forum.com/t/upload-test-images-using-rake-with-populator/154843 "2008-12-04T12:34:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 4, 2008, 12:34pm UTC](https://www.ruby-forum.com/t/upload-test-images-using-rake-with-populator/154843/1 "2008-12-04T12:34:26Z")

</div>

I am wondering if it is possible to upload test images to the database  
along with the other fake user data. I am using populator along with  
faker for fake test data and it is working very well. I am able to do  
things like:

```
User.populate 100 do |user|
  user.username = Faker::Name.first_name
  user.email = Faker::Internet.email

```

end

but I am wondering if theres a way to do something like:

```
User.populate 100 do |user|
  user.username = Faker::Name.first_name
  user.email = Faker::Internet.email
 Photo.populate 1 do |photo|
   photo.user_id = user.id ....

```

end

I know this is not possible with populator, but maybe someone can  
enlighten me with the direction towards a function I can write on my  
own? I am using attachment\_fu now, but I am looking to switch to  
paperclip in the near future. Also if anyone has other methods  
excluding rake tasks for doing this please let me know. thanks.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 4, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/upload-test-images-using-rake-with-populator/154843/2 "2008-12-04T12:54:31Z")

</div>

Just found this relevant code online. Gonna play around with this and  
see where I get:

```
namespace :avatars do
  desc "Create initial avatar database."
  task :init => :environment do
    Dir.chdir("#{RAILS_ROOT}/public/avatars/")
    @image_paths = Dir["*.jpg"]
    for avatar in @image_paths
      path = "#{RAILS_ROOT}/public/avatars/#{avatar}"
      fsize = File.size(path)
      avatar = Avatar.create(:avatar_file_name =>

```

avatar, :avatar\_content\_type =\> ‘image/jpeg’, :avatar\_file\_size =\>  
fsize)  
avatar.save  
end  
end  
end
