0

I have a blob column in a table that I know represents pdf documents. I am attempting to write a migration that rips out the pdf documents from that blob field, and saves the actual pdf documents within public/assets. I am using paperclip for attachments.

Here is the error I am getting:

StandardError: An error has occurred, all later migrations canceled: "\xC4" from ASCII-8BIT to UTF-8

Here is my script:

class AddSomeAttachments < ActiveRecord::Migration
  def up

    SomeModel.all.each do |something|
      if something.data.present?
        FileUtils.mkdir_p(Rails.root.join('public', 'assets', 'some_models', 'attachment1', "#{something.id}" ))
      end
    end

    SomeModel.all.each do |something|
      if something.data.present?
        File.open(Rails.root.join('public', 'assets', 'some_models', 'attachment1', "#{something.id}", "#{something.attachment1_file_name}"), "w+") do |file|
          file << something.data
        end
      end
    end

  end

  def down
    raise "do not migrate down"
  end
end

I did look at this stack overflow question which asked about the same error message. I did attempt adding encoding: UTF-8 at the top of the file but that did not do anything.

Community
  • 1
  • 1
Neil
  • 4,578
  • 14
  • 70
  • 155

1 Answers1

1

Try writing a binary file:

File.open(filename, 'wb') do |file|
Kris
  • 19,188
  • 9
  • 91
  • 111