我有 2 个模型——文档和附件。我正在尝试将大量文件导入我的 heroku 数据库。我也有这方面的任务。当我使用本地数据库运行此任务时,它工作正常,但是当我尝试在阶段 env 中运行它时,“document.save”操作出现错误:
$ RAILS_ENV=stage thor import:documents '/path/to/files/'
/../gems/fog-1.10.0/lib/fog/core/hmac.rb:23:in `digest': can't convert Fixnum into String (TypeError)
此外,当我通过 heroku 应用程序上的表单上传文件时,一切正常。所以有什么问题?
以下是模型:
附件:
class Attachment < ActiveRecord::Base
IMAGE_TYPES = ['jpg', 'jpeg', 'gif', 'png']
DOC_TYPES = ['pdf', 'doc', 'docx', 'rtf', 'pages', 'txt']
belongs_to :attachable, polymorphic: true
attr_accessible :attachment_file, :attachment_file_cache, :attachment_type
mount_uploader :attachment_file, AttachmentUploader
validates :attachment_file, presence: true
...
end
文档:
class Document < ActiveRecord::Base
attr_accessible :description, :title, :visible, :attachment_attributes
has_one :attachment, as: :attachable, dependent: :destroy, class_name: 'Attachment', conditions: { attachment_type: 'document' }
...
end
雷神任务:
class Import < Gearup::TasksBase
desc "documents <DOCUMENTS_FOLDER>", 'Upload documents'
def documents(dir_path)
dir_path += "#{dir_path.last == '/' ? '*' : '/*'}"
print_color_message('! Directory is empty !', 'red') if Dir[dir_path].empty?
Dir[dir_path].each do |file_path|
document = Document.new(title: 'document_title')
document.build_attachment(attachment_type: 'document', attachment_file: File.open(file_path))
if document.save ### HERE IS A PROBLEM ###
print_color_message("-= document \"#{document.title}\" successfully created =-", 'green')
else
print_color_message("! document not saved !", 'red')
print_color_message(document.errors.messages.inspect, 'red')
end
end
end
end
而且我没有忘记编辑我的database.yml
heroku pg:credentials HEROKU_POSTGRESQL_ORANGE_URL
数据库.yml
...
stage:
adapter: postgresql
encoding: unicode
database: <database_name>
host: <host_name>
sslmode: <sslmode>
port: <port>
pool: <pool>
username: <username>
password: <password>
谢谢。