如何将 category_ids 从 excel 文件导入到 categories_products 并在 rails 中加入 table?
How can I import category_ids from excel file to categories_products join table in rails ?
我在列名 category_ids 中有 excel 文件。我需要从 Rails 中的 excel 文件导入以加入 table、categories_products
。
我该怎么办?
desc 'import data for models'
task datas: [:environment] do
categories_sheet = sheet.sheet('category')
(2..categories_sheet.last_row).each do |j|
category_row = categories_sheet.row(j)
category = Category.create!(id: category_row[0], name:
category_row[1])
print category.id
categories_sheet = sheet.sheet('category')
end
product_sheet = sheet.sheet('product')
(2..product_sheet.last_row).each do |j|
product_row = product_sheet.row(j)
product = Product.create!(id: product_row[0],
name: product_row[1])
category_ids = product_row[2].split(",")
product.category_ids = category_ids
product_sheet = sheet.sheet('product')
end
end
我在列名 category_ids 中有 excel 文件。我需要从 Rails 中的 excel 文件导入以加入 table、categories_products
。
我该怎么办?
desc 'import data for models'
task datas: [:environment] do
categories_sheet = sheet.sheet('category')
(2..categories_sheet.last_row).each do |j|
category_row = categories_sheet.row(j)
category = Category.create!(id: category_row[0], name:
category_row[1])
print category.id
categories_sheet = sheet.sheet('category')
end
product_sheet = sheet.sheet('product')
(2..product_sheet.last_row).each do |j|
product_row = product_sheet.row(j)
product = Product.create!(id: product_row[0],
name: product_row[1])
category_ids = product_row[2].split(",")
product.category_ids = category_ids
product_sheet = sheet.sheet('product')
end
end