将值存储在字符串类型字段中,该字段将值存储为数组

时间:2019-05-15 19:53:03

标签: ruby-on-rails rake-task

我正在创建多个附件导入的工具,我无法弄清的唯一问题是如何将文件名存储在文档字段中,因为它将文件名存储为数组。

这就是我要尝试的事情,

upload.rake

require 'fileutils'
namespace :request_document do
  desc "Expects to get a file or folder ..."
  task :import_attachment, [:filename] => [:environment] do |task, args|

    source_directory = "/Users/admin/Documents/demo"

    Dir.foreach(source_directory) do |filename|
      if /^(?<client_id>\d+)/ =~ filename
        puts filename
        client = Client.find_by(
          legacy_client_id: client_id
        )
        if client.nil?
          puts "Client Not Found"
        else
          puts "Client found"
          request = client.requests.last
          # document_uploader = DocumentUploader.new
          # File.open("#{source_directory}/#{filename}") { |f| document_uploader.store!(f) }
          # document_uploader.store!("#{source_directory}/#{filename}")
          target_directory = "public/uploads/request/documents/#{request.id}"
          puts request.id
          FileUtils.mkdir_p(target_directory)
          FileUtils.cp("#{source_directory}/#{filename}", "#{target_directory}")
          request.documents.push(['filename'])
          request.save
        end
      end
    end 
  end
end

请求模式

create_table "requests", force: :cascade do |t|
  t.string "documents", default: [], array: true
end

我正在使用载波宝石来上传多个图像。

我得到的错误是:

NoMethodError: undefined method `identifier' for ["filename"]:Array

1 个答案:

答案 0 :(得分:0)

我以前用载波见过一次。我花了很长时间才弄清楚,但是模型属性和载波安装点之间有些重复。引用它的here

载波回购中有一个封闭的问题