我正在开发一个Rails应用程序,我在S3中有一个用于活动存储的has_one_attached个人资料图片,当我为我的用户播种时,我想从Faker中为他们的头像图像播种数据。以下是种子代码:
20.times do
Student.create(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
phone_number: Faker::PhoneNumber.cell_phone,
school_id: School.first.id,
vehicle_make_model: Faker::Vehicle.make_and_model,
vehicle_year: Faker::Vehicle.year,
vehicle_color: Faker::Vehicle.color,
email_address: Faker::Internet.email,
password: "password",
email: Faker::Internet.email,
profile_picture: Faker::Avatar.image
).save
end这是用户的模型,也就是学生模型(这是正确设置的,并且在通过网站上传图像时有效):
class Student < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
belongs_to :school, optional: true
has_many :offers
has_many :comments
has_many :cars
has_one_attached :profile_picture
end我以前已经播种了其余的数据,它工作得很好,但是一旦我集成了S3并尝试播种这些数据,它就不再对我起作用了。
发布于 2018-12-01 01:38:02
尝试如下所示:
#seed_file
require 'open-uri'
def image_fetcher
open(Faker::Avatar.image)
rescue
open("https://robohash.org/sitsequiquia.png?size=300x300&set=set1")
end
20.times do |n|
s = Student.create(
...
#remove profile_picture from here
)
s.profile_picture.attach({
io: image_fetcher,
filename: "#{n}_faker_image.jpg"
})
end更新:
require 'faker'
require 'open-uri'
def image_fetcher
URI.open(Faker::Avatar.image)
rescue
URI.open("https://robohash.org/sitsequiquia.png?size=300x300&set=set1")
endhttps://stackoverflow.com/questions/53562171
复制相似问题