Guest User

Untitled

a guest
Jun 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. image = Image.create(:id => 42, :filename => "foo.jpg")
  2. image.id #=> 42
  3.  
  4. Dir.glob("/images/to/import/*.{jpg,png,gif}").each do |path|
  5.  
  6. # simulate uploading the image
  7. tempfile = Tempfile.new(path)
  8. tempfile.set_encoding(Encoding::BINARY) if tempfile.respond_to?(:set_encoding)
  9. tempfile.binmode
  10. FileUtils.copy_file(path, tempfile.path)
  11.  
  12. # create as you do in the controller - may need other metadata here
  13. image = Image.create({:uploaded_data => tempfile})
  14. unless image.save
  15. logger.info "Failed to save image #{path} in migration: #{image.errors.full_messages}"
  16. end
  17.  
  18. tempfile.close!
  19. end
  20.  
  21. class AddImages < ActiveRecord::Migration
  22. def self.up
  23. Image.destroy_all
  24.  
  25. execute("ALTER TABLE images AUTO_INCREMENT = 1")
  26.  
  27. image = Image.create(:filename => "foo.jpg")
  28. image.id #=> 1
  29. end
  30.  
  31. def self.down
  32. end
  33. end
Add Comment
Please, Sign In to add comment