I have 2 models – Document and Attachment. I'm trying to import a lot of files to my heroku db. I have also thor task for it. It works fine when I run this task with my local db, but when I try to run it in stage env, I have an error at "document.save" action:
$ 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)
Also, when I upload file via form on heroku app, everything works great. So what's the problem?
Here are models:
Attachment:
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
Document:
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
Thor task:
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
And I didn't forget to edit my database.yml with
heroku pg:credentials HEROKU_POSTGRESQL_ORANGE_URL
database.yml
...
stage:
adapter: postgresql
encoding: unicode
database: <database_name>
host: <host_name>
sslmode: <sslmode>
port: <port>
pool: <pool>
username: <username>
password: <password>
Thanks.
Possibly you forgot to setup s3 credentials in your config. Heroku takes it from ENV.