Forum: Ruby on Rails upload test images using rake with populator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F677fa685a2cfe8aff31f161062db3d3?d=identicon&s=25 David (Guest)
on 2008-12-04 12:34
(Received via mailing list)
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.
F677fa685a2cfe8aff31f161062db3d3?d=identicon&s=25 David (Guest)
on 2008-12-04 12:54
(Received via mailing list)
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
This topic is locked and can not be replied to.