Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- image = Image.create(:id => 42, :filename => "foo.jpg")
- image.id #=> 42
- Dir.glob("/images/to/import/*.{jpg,png,gif}").each do |path|
- # simulate uploading the image
- tempfile = Tempfile.new(path)
- tempfile.set_encoding(Encoding::BINARY) if tempfile.respond_to?(:set_encoding)
- tempfile.binmode
- FileUtils.copy_file(path, tempfile.path)
- # create as you do in the controller - may need other metadata here
- image = Image.create({:uploaded_data => tempfile})
- unless image.save
- logger.info "Failed to save image #{path} in migration: #{image.errors.full_messages}"
- end
- tempfile.close!
- end
- class AddImages < ActiveRecord::Migration
- def self.up
- Image.destroy_all
- execute("ALTER TABLE images AUTO_INCREMENT = 1")
- image = Image.create(:filename => "foo.jpg")
- image.id #=> 1
- end
- def self.down
- end
- end
Add Comment
Please, Sign In to add comment