gpt4 book ai didi

ruby-on-rails - 主动存储种子 Rails

转载 作者:数据小太阳 更新时间:2023-10-29 07:23:32 24 4
gpt4 key购买 nike

我想在我的数据库中植入一些包含事件存储附件的实例,但我不知道该怎么做。我尝试了一些方法但没有成功。

这是我的种子。

User.create(email: "test@ok.com", password: "okokok") if User.count.zero?

50.times do |i|
temp = Template.create(
title: Faker::Name.name,
description: Faker::Lorem.paragraph(2),
user: User.first,
github_link: Faker::SiliconValley.url,
category: rand(0...4)
)
puts Template.first.photo
temp.photo.attach(Template.first.photo)
end

谢谢你的帮助

最佳答案

这几天也在文档指南中:

http://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-file-io-objects

Sometimes you need to attach a file that doesn’t arrive via an HTTP request. For example, you may want to attach a file you generated on disk or downloaded from a user-submitted URL. You may also want to attach a fixture file in a model test. To do that, provide a Hash containing at least an open IO object and a filename:

@message.image.attach(io: File.open('/path/to/file'), filename: 'file.pdf')

When possible, provide a content type as well. Active Storage attempts to determine a file’s content type from its data. It falls back to the content type you provide if it can’t do that.

@message.image.attach(io: File.open('/path/to/file'), filename: 'file.pdf', content_type: 'application/pdf')

If you don’t provide a content type and Active Storage can’t determine the file’s content type automatically, it defaults to application/octet-stream.

关于ruby-on-rails - 主动存储种子 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50087938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com