0

我有一个上传文本文件的应用程序,然后将其文件名、内容类型和数据保存到其数据库表中。

这一切都很好,但是,现在我正在尝试创建一种方法,将文件拆分为单词,将单词拆分为字母,按字母顺序对它们进行排序,然后再次加入它们,同时将排序的字母作为键输入到哈希中用字母表示的单词作为值。

"denoops"=>[" snooped", "spooned"]

这适用于小文件,但是当解析一个 300,000 字左右的大文件时,它会完成将所有数据输入到哈希中,但随后会出错:

expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS

这是代码。注意更新我添加了序列化,它将数据作为哈希保存到数据库,并将列类型从二进制更改为文本。

附件.rb:

 class Attachment < ActiveRecord::Base
    has_many :anagrams, dependent: :destroy
    attr_accessible :filename, :content_type, :data
    validates_presence_of :filename, :data
    serialize :data, Hash


    def uploaded_file=(incoming_file)
      self.filename = incoming_file.original_filename
      self.content_type = incoming_file.content_type
      results = {}

      incoming_file.read.downcase.split.each do |word|
        letters = word.split('').sort
        results[letters.join] = [[results[letters]].join(" ")] << word
        end
      self.data = results
    end

导轨错误日志:

 Started POST "/attachments" for 127.0.0.1 at 2012-08-20 17:10:09 +0100
Processing by AttachmentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"y2S3Z6Yg9iuU74to7ZEDUedlCEsazEU26mIrsnNxgmQ=", "attachment"=>#<ActionDispatch::Http::UploadedFile:0x007f8a411497b8 @original_filename="medium_word_list.90", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"attachment\"; filename=\"medium_word_list.90\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/1g/d9qbm7_s0_5fcljtvzysp1gc0000gn/T/RackMultipart20120820-3535-119x9hk>>, "commit"=>"upload"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 11177ms

RuntimeError (expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS):
  app/controllers/attachments_controller.rb:21:in `create'

创建附件表迁移:

class CreateAttachments < ActiveRecord::Migration
  def up
    create_table :attachments do |t|
      t.string :filename
      t.string :content_type
      t.text :data
    end
  end

Google 提供的内容不多。如果有人需要更多代码,请说。

4

1 回答 1

1

对您的方法进行一些重构和重组,uploaded_file=如下所示:

def uploaded_file=(incoming_file)
  self.filename = incoming_file.original_filename
  self.content_type = incoming_file.content_type
  results = {}

  incoming_file.read.downcase.split.each do |word|
    letters = word.split('').sort
    results[
      letters
    ] = [
      [
        results[
          letters
        ]
      ].join(" ")
    ] << word 
  end

  self.data = results
end

对哈希的分配results看起来很可疑。您正在分配一个数组作为哈希键。然后,您尝试保存它。我怀疑问题是该数组作为键。试试这个:

results[
  letters.join
] = [
于 2012-08-20T13:25:11.283 回答