-
Notifications
You must be signed in to change notification settings - Fork 1.7k
How to: "Upload" from a local file
Qichunren edited this page May 20, 2019
·
8 revisions
It's easy to "upload" a file programmatically, for test or fixture or seed or scripting purposes.
For instance, do this inside seed.rb
to create a Restaurant
instance with a CarrierWave logo
field from an image you've got stored away in your own db/images
directory:
restaurant = Restaurant.create!(name: "McDonald's")
restaurant.logo = Pathname.new(Rails.root.join("db/images/mcdonalds_logo.png")).open
restaurant.save!
If you are using multiple file uploads (e.g: using a Postgresql Array field), you should be able to upload programmatically using:
restaurant = Restaurant.create!(name: "McDonald's")
restaurant.logos = [
Pathname.new("/path/to/file/logo_1.jpg").open,
Pathname.new("/path/to/file/logo_2.jpg").open
]
restaurant.save!
Load all jpeg
match Filename == id
from some local path
@images = Dir.glob("some/local/path/*.jpeg")
@images.each do |i|
id = File.basename(i).gsub('.jpeg','');
t = Product.where(id:id).first;
t.images = [Pathname.new(i).open];
end