0
我正在使用roo庫解析一個.xls文件並填充我的模型。我正在使用seeds.rb文件和using rake db:seed
命令。我收到一個錯誤,說我試圖打開的文件不存在,但它位於seed.rb的相同文件夾中。這是迄今爲止我所得到的。無法使用rake數據庫種子加載文件來使用roo庫填充數據庫
require 'rubygems'
require 'roo'
require 'active_record'
co = Roo::Excel.new("excel.xls")
co.default_sheet = co.sheets.first
2.upto(42700) do |line|
#Category
category_xls = co.cell(line, 'A')
category_id = category_xls[0,2].to_i
category_name = category_xls[4, category_xls.length]
#Subcategory
subcategory_xls = co.cell(line, 'B')
#Item
item_xls = co.cell(line, 'C')
partno_xls = co.cell(line, 'D')
description_xls = co.cell(line, 'E')
explanation_xls = co.cell(line, 'H')
information_xls = co.cell(line, 'J')
#Uom
uom_xls = co.cell(line, 'F')
#MTML
mtml_xls = co.cell(line, 'G')
#Picture
#picture = co.cell(line, 'I')
# cria uma categoria e a insere no banco, caso ela nao exista
category = Category.find_or_create_by_category_name(category_number: category_id, category_name: category_name)
# cria uma subcategoria e a insere no banco, caso ela nao exista
subcategory = Subcategory.find_or_create_by_subcategory_name(subcategory_name: subcategory_xls, category: category)
# cria um item
item = Item.create(item_name: item_xls, explanation: explanation_xls, information: information_xls, description: description_xls, subcategory: subcategory)
# cria um uom
uom = Uom.find_or_create_by_uom_name(uom_name: uom_xls, item: item)
# cria um mtml
mtml = Mtml.find_or_create_by_mtml_name(mtml_name: mtml_xls, item: item)
end