我有 2 个模型——文档和附件。我正在尝试将大量文件导入到我的 heroku 数据库中。我也有雷神任务。当我用我的本地数据库运行这个任务时它工作正常,但是当我尝试在 stage 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.ymlheroku 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>
谢谢。
最佳答案
可能您忘记在配置中设置 s3 凭据。 Heroku 从 ENV 中获取它。
关于ruby-on-rails - Rails – 如何将文件从本地主机导入到 heroku db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18826642/