0

嘿,我正在尝试将一个 excel 文件从我的工作表导入数据库,但我不知道某些东西无法正常工作,获取未知属性:

我的表是这样的:

 name   title   jalebi  samosa  s1  s2  s3  
   1    4        7       7      7    7   7  
   2    5        6       6      6    6   6  
   3    6        5       5      5    5   5  
   4    7        4       4      4    4   4  

我的迁移是:

      class CreateTest3s < ActiveRecord::Migration[5.1]
    def change
      create_table :test3s do |t|
        t.string :name
        t.string :title
        t.string :jalebi
        t.string :samosa
        t.string :s1
        t.string :s2
        t.string :s3

        t.timestamps
      end
    end
  end

我在模型中的导入代码是:

    def self.import1(file)

   spreadsheet = Roo::Spreadsheet.open(file.path)
          header = spreadsheet.row(1)
          (2..spreadsheet.last_row).each do |i|
            row = Hash[[header, spreadsheet.row(i)].transpose]
            puts row.to_hash
            product = find_by(id: row["id"]) || new
            product.attributes = row.to_hash
            product.save!
          end

 end

每当我尝试运行它时,我都会收到此错误:

 unknown attribute 'samosa ' for Test3.

哈希值是这样的:

 {"name"=>1, "title"=>4, "jalebi"=>7, "samosa "=>9, "s1"=>7, "s2"=>7, "s3"=>7}
4

1 回答 1

1

看起来您尝试设置的属性"samosa "带有尾随空格。该属性应该只是"samosa"没有空格。我猜你的电子表格标题是空间的来源。

于 2017-10-19T12:44:28.723 回答