我有一个 excel 文件导入器。我想从具有属性名称的列名的 excel 表中导入记录。
class DDImporter
def initialize(path)
@path = path
end
def extract sheet_name
file = Roo::Excelx.new(@path)
file.default_sheet = sheet_name
header = file.row 1
2.upto(file.last_row) do |i|
row = Hash[[header, file.row(i)].transpose]
row.delete "id"
# row => ['name', 'price', 'product_id']
sheet_name.classify.constantize.where(name: row['name']).first_or_create # I need to put attributes hash here
end
end
end