【发布时间】:2013-09-16 11:18:10
【问题描述】:
我有 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.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>
谢谢。
【问题讨论】:
-
您是否尝试直接上传到 heroku,因为我不相信 heroku 允许您在本地存储文件。
-
我相信 kenjione 正在尝试将文件上传到 amazon-s3 存储,使用carrierwave +雾,从本地机器,运行带有RAILS_ENV=stage 的thor 任务(允许连接到远程数据库)。跨度>
标签: ruby-on-rails heroku amazon-s3 carrierwave thor