使用虚假数据生成回形针图像上传 - Ruby on Rails Populator/Faker Gems [英] Generating Paperclip image uploads with fake data - Ruby on Rails Populator / Faker Gems

查看:37
本文介绍了使用虚假数据生成回形针图像上传 - Ruby on Rails Populator/Faker Gems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试用一堆虚假数据填充一个项目的开发数据库,​​以模拟数百篇文章/用户的外观和操作.我研究了不同的宝石来完成这项任务——比如工厂女孩,但文档非常缺乏而且我没有得到它——但最终使用了 Populator 和 Faker 宝石并完成了以下任务......

I am currently trying to populate a development database on a project with a bunch of fake data, to simulate how it will look and operate with hundreds of articles / users. I looked into different gems to do the task - such as Factory Girl, but documentation was very lacking and I didn't get it - but ended up using the Populator and Faker gems and did the following rake task...

namespace :db do
   desc "Testing populator"
   task :populate => :environment do
      require "populator"
      require "faker"

      User.populate 3 do |user|
         name = Faker::Internet.user_name   
         user.name = name
         user.cached_slug = name
         user.email = Faker::Internet.email
         user.created_at = 4.years.ago..Time.now
      end
   end
end

效果很好...适用于基于文本的数据.但是,所有用户都有一个可以通过回形针附件上传的头像,所有常规内容都以相同的方式具有缩略图附件.

Works great...for text based data. However, all users have an avatar that can be uploaded via Paperclip attachment, as well as all regular content have thumbnail attachments in the same manner.

我知道 Populator gem 只是直接向数据库填充,不一定要通过 ActiveRecord 验证来执行此操作..因此我认为 Paperclip 无法运行以生成所有不同的缩略图和需要(并上传到适当的目录)作为头像,如果我只是在上面的 rake 任务中用文件路径填充该字段.

I understand the Populator gem simply does a straight population to the database and not necessarily running through ActiveRecord validations to do so..therefor I would assume Paperclip can't run to generate all the different thumbnails and needed (and uploaded to the proper directory) for the avatar if I just filled the field with a filepath in the rake task above.

有没有办法通过 Populator 或其他方式填充假图像?或者也许是一种将 rake 任务指向我硬盘驱动器上的库存图像目录以自动为每条记录自动生成随机缩略图的方法?在 Google 上寻找了一种方法,但没有找到有关该主题的太多信息.

Is there a way to populate fake images, via Populator or another way? Or perhaps a way to point the rake task at a directory of stock images on my hard drive to autogenerate random thumbnails for each record? Took a hunt on Google for a way, but have not turned up much information on the subject.

更新

最终的解决方案,基于pwnfactory的思路...

The final solution, based on pwnfactory's line of thinking...

namespace :db do
  desc "Testing populator"
  task :populate => :environment do
    require "populator"
    require "faker"

    User.populate 3 do |user|
      name = Faker::Internet.user_name
      user.name = name
      user.cached_slug = name
      user.email = Faker::Internet.email
      user.created_at = 4.years.ago..Time.now
    end

    User.all.each { |user| user.avatar = File.open(Dir.glob(File.join(Rails.root, 'sampleimages', '*')).sample); user.save! }
  end
end

它基本上循环并从所有记录上的 sampleimages 目录上传头像.

It basically loops back around and uploaded avatars from the sampleimages directory on all the records.

推荐答案

要将随机图像关联到您的任务中,您可以尝试以下操作:

To associate a random image in your task, you could try the following:

user.avatar = File.open(Dir.glob(File.join(Rails.root, 'sampleimages', '*')).sample)

其中 sampleimages 是包含要随机关联的头像的目录

where sampleimages is a directory containing avatars to be associated at random

这篇关于使用虚假数据生成回形针图像上传 - Ruby on Rails Populator/Faker Gems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆