0
嘿,我想從我的表到數據庫中導入一個Excel文件,但我不知道自己是不是工作 獲得未知屬性:未知屬性5
我的表是這樣的:
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}
你能告訴你如何定義'Test3'模型嗎? 'import1'方法在哪個類中?哪行代碼會產生'unknown attribute'錯誤? – hoffm