I have 2 models - a document and an application. I am trying to import a large number of files into my db heroin. I have what I need for this. It works fine when I run this task using my local db, but when I try to run it at the env stage, I have an error with the 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 a file through a form in a heroku application, everything works fine. So what is the problem?
Here are the 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
And I did not forget to edit my .yml database 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.